|
|
|
|
@ -10,9 +10,11 @@ import (
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
|
|
|
|
"net/http"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
metadata "cloud.google.com/go/compute/metadata"
|
|
|
|
|
compute "google.golang.org/api/compute/v1"
|
|
|
|
|
"google.golang.org/api/option"
|
|
|
|
|
oslogin "google.golang.org/api/oslogin/v1"
|
|
|
|
|
@ -32,6 +34,7 @@ import (
|
|
|
|
|
type driverGCE struct {
|
|
|
|
|
projectId string
|
|
|
|
|
service *compute.Service
|
|
|
|
|
thisGCEUser string
|
|
|
|
|
osLoginService *oslogin.Service
|
|
|
|
|
ui packersdk.Ui
|
|
|
|
|
}
|
|
|
|
|
@ -119,6 +122,9 @@ func NewClientOptionGoogle(account *ServiceAccount, vaultOauth string, impersona
|
|
|
|
|
// 4. On Google Compute Engine and Google App Engine Managed VMs, it fetches
|
|
|
|
|
// credentials from the metadata server.
|
|
|
|
|
// (In this final case any provided scopes are ignored.)
|
|
|
|
|
//
|
|
|
|
|
// Note: (4) is not usable with OSLogin on Google Compute Engine (GCE).
|
|
|
|
|
// The GCE service account is derived separately and used instead.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
@ -129,6 +135,9 @@ func NewClientOptionGoogle(account *ServiceAccount, vaultOauth string, impersona
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewDriverGCE(config GCEDriverConfig) (Driver, error) {
|
|
|
|
|
|
|
|
|
|
var thisGCEUser string
|
|
|
|
|
|
|
|
|
|
opts, err := NewClientOptionGoogle(config.Account, config.VaultOauthEngineName, config.ImpersonateServiceAccountName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
@ -140,6 +149,15 @@ func NewDriverGCE(config GCEDriverConfig) (Driver, error) {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if metadata.OnGCE() {
|
|
|
|
|
log.Printf("[INFO] On GCE, capture service account for OSLogin...")
|
|
|
|
|
thisGCEUser, err = metadata.NewClient(&http.Client{}).Email("")
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.Printf("[INFO] Instantiating OS Login client...")
|
|
|
|
|
osLoginService, err := oslogin.NewService(context.TODO(), opts)
|
|
|
|
|
if err != nil {
|
|
|
|
|
@ -152,6 +170,7 @@ func NewDriverGCE(config GCEDriverConfig) (Driver, error) {
|
|
|
|
|
return &driverGCE{
|
|
|
|
|
projectId: config.ProjectId,
|
|
|
|
|
service: service,
|
|
|
|
|
thisGCEUser: thisGCEUser,
|
|
|
|
|
osLoginService: osLoginService,
|
|
|
|
|
ui: config.Ui,
|
|
|
|
|
}, nil
|
|
|
|
|
@ -625,8 +644,13 @@ func (d *driverGCE) getPasswordResponses(zone, instance string) ([]windowsPasswo
|
|
|
|
|
return passwordResponses, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *driverGCE) GetOSLoginUserFromGCE() string {
|
|
|
|
|
return d.thisGCEUser
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *driverGCE) ImportOSLoginSSHKey(user, sshPublicKey string) (*oslogin.LoginProfile, error) {
|
|
|
|
|
parent := fmt.Sprintf("users/%s", user)
|
|
|
|
|
|
|
|
|
|
resp, err := d.osLoginService.Users.ImportSshPublicKey(parent, &oslogin.SshPublicKey{
|
|
|
|
|
Key: sshPublicKey,
|
|
|
|
|
}).Do()
|
|
|
|
|
|