1
0
mirror of https://github.com/rancher/os.git synced 2025-09-04 08:14:21 +00:00

Fix fetching SSH authorized keys from GCE

This commit is contained in:
Ivan Mikushin
2016-06-29 20:32:49 -07:00
parent 93b4949cce
commit e6a550d786
2 changed files with 49 additions and 22 deletions

View File

@@ -37,13 +37,13 @@ import (
"github.com/coreos/coreos-cloudinit/datasource/file"
"github.com/coreos/coreos-cloudinit/datasource/metadata/digitalocean"
"github.com/coreos/coreos-cloudinit/datasource/metadata/ec2"
"github.com/coreos/coreos-cloudinit/datasource/metadata/gce"
"github.com/coreos/coreos-cloudinit/datasource/metadata/packet"
"github.com/coreos/coreos-cloudinit/datasource/proc_cmdline"
"github.com/coreos/coreos-cloudinit/datasource/url"
"github.com/coreos/coreos-cloudinit/pkg"
"github.com/coreos/coreos-cloudinit/system"
"github.com/rancher/netconf"
"github.com/rancher/os/cmd/cloudinit/gce"
rancherConfig "github.com/rancher/os/config"
"github.com/rancher/os/util"
)

View File

@@ -1,23 +1,11 @@
// Copyright 2016 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gce
import (
"fmt"
"net"
"net/http"
"strconv"
"strings"
"github.com/coreos/coreos-cloudinit/datasource"
"github.com/coreos/coreos-cloudinit/datasource/metadata"
@@ -25,7 +13,7 @@ import (
const (
apiVersion = "computeMetadata/v1/"
metadataPath = apiVersion + "instance/"
metadataPath = apiVersion
userdataPath = apiVersion + "instance/attributes/user-data"
)
@@ -38,23 +26,48 @@ func NewDatasource(root string) *metadataService {
}
func (ms metadataService) FetchMetadata() (datasource.Metadata, error) {
public, err := ms.fetchIP("network-interfaces/0/access-configs/0/external-ip")
public, err := ms.fetchIP("instance/network-interfaces/0/access-configs/0/external-ip")
if err != nil {
return datasource.Metadata{}, err
}
local, err := ms.fetchIP("network-interfaces/0/ip")
local, err := ms.fetchIP("instance/network-interfaces/0/ip")
if err != nil {
return datasource.Metadata{}, err
}
hostname, err := ms.fetchString("hostname")
hostname, err := ms.fetchString("instance/hostname")
if err != nil {
return datasource.Metadata{}, err
}
projectSshKeys, err := ms.fetchString("project/attributes/sshKeys")
if err != nil {
return datasource.Metadata{}, err
}
instanceSshKeys, err := ms.fetchString("instance/attributes/sshKeys")
if err != nil {
return datasource.Metadata{}, err
}
keyStrings := strings.Split(projectSshKeys+"\n"+instanceSshKeys, "\n")
sshPublicKeys := map[string]string{}
i := 0
for _, keyString := range keyStrings {
keySlice := strings.SplitN(keyString, ":", 2)
if len(keySlice) == 2 {
key := strings.TrimSpace(keySlice[1])
if key != "" {
sshPublicKeys[strconv.Itoa(i)] = strings.TrimSpace(keySlice[1])
i++
}
}
}
return datasource.Metadata{
PublicIPv4: public,
PrivateIPv4: local,
Hostname: hostname,
PublicIPv4: public,
PrivateIPv4: local,
Hostname: hostname,
SSHPublicKeys: sshPublicKeys,
}, nil
}
@@ -87,3 +100,17 @@ func (ms metadataService) fetchIP(key string) (net.IP, error) {
return nil, fmt.Errorf("couldn't parse %q as IP address", str)
}
}
func (ms metadataService) FetchUserdata() ([]byte, error) {
data, err := ms.FetchData(ms.UserdataUrl())
if err != nil {
return nil, err
}
if len(data) == 0 {
data, err = ms.FetchData(ms.MetadataUrl() + "instance/attributes/startup-script")
if err != nil {
return nil, err
}
}
return data, nil
}