func NewDriverGCE(ui packer.Ui, p string, a *accountFile) (Driver, error) { var err error var client *http.Client // Auth with AccountFile first if provided if a.PrivateKey != "" { log.Printf("[INFO] Requesting Google token via AccountFile...") log.Printf("[INFO] -- Email: %s", a.ClientEmail) log.Printf("[INFO] -- Scopes: %s", DriverScopes) log.Printf("[INFO] -- Private Key Length: %d", len(a.PrivateKey)) conf := jwt.Config{ Email: a.ClientEmail, PrivateKey: []byte(a.PrivateKey), Scopes: DriverScopes, TokenURL: "https://accounts.google.com/o/oauth2/token", } // Initiate an http.Client. The following GET request will be // authorized and authenticated on the behalf of // your service account. client = conf.Client(oauth2.NoContext) } else { log.Printf("[INFO] Requesting Google token via GCE Service Role...") client = &http.Client{ Transport: &oauth2.Transport{ // Fetch from Google Compute Engine's metadata server to retrieve // an access token for the provided account. // If no account is specified, "default" is used. Source: google.ComputeTokenSource(""), }, } } if err != nil { return nil, err } log.Printf("[INFO] Instantiating GCE client...") service, err := compute.New(client) // Set UserAgent versionString := version.FormattedVersion() service.UserAgent = fmt.Sprintf( "(%s %s) Packer/%s", runtime.GOOS, runtime.GOARCH, versionString) if err != nil { return nil, err } return &driverGCE{ projectId: p, service: service, ui: ui, }, nil }
func NewDriverGCE(ui packer.Ui, p string, a *AccountFile) (Driver, error) { var err error var client *http.Client // Auth with AccountFile first if provided if a.PrivateKey != "" { log.Printf("[INFO] Requesting Google token via AccountFile...") log.Printf("[INFO] -- Email: %s", a.ClientEmail) log.Printf("[INFO] -- Scopes: %s", DriverScopes) log.Printf("[INFO] -- Private Key Length: %d", len(a.PrivateKey)) conf := jwt.Config{ Email: a.ClientEmail, PrivateKey: []byte(a.PrivateKey), Scopes: DriverScopes, TokenURL: "https://accounts.google.com/o/oauth2/token", } // Initiate an http.Client. The following GET request will be // authorized and authenticated on the behalf of // your service account. client = conf.Client(oauth2.NoContext) } else { log.Printf("[INFO] Requesting Google token via GCE API Default Client Token Source...") client, err = google.DefaultClient(oauth2.NoContext, DriverScopes...) // The DefaultClient uses the DefaultTokenSource of the google lib. // The DefaultTokenSource uses the "Application Default Credentials" // It looks for credentials in the following places, preferring the first location found: // 1. A JSON file whose path is specified by the // GOOGLE_APPLICATION_CREDENTIALS environment variable. // 2. A JSON file in a location known to the gcloud command-line tool. // On Windows, this is %APPDATA%/gcloud/application_default_credentials.json. // On other systems, $HOME/.config/gcloud/application_default_credentials.json. // 3. On Google App Engine it uses the appengine.AccessToken function. // 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.) } if err != nil { return nil, err } log.Printf("[INFO] Instantiating GCE client...") service, err := compute.New(client) // Set UserAgent versionString := version.FormattedVersion() service.UserAgent = fmt.Sprintf( "(%s %s) Packer/%s", runtime.GOOS, runtime.GOARCH, versionString) if err != nil { return nil, err } return &driverGCE{ projectId: p, service: service, ui: ui, }, nil }
"github.com/Azure/azure-sdk-for-go/arm/resources/subscriptions" "github.com/Azure/go-autorest/autorest" "github.com/Azure/go-autorest/autorest/azure" "github.com/Azure/go-autorest/autorest/to" "github.com/mitchellh/go-homedir" "github.com/mitchellh/packer/version" ) var ( // AD app id for packer-azure driver. clientIDs = map[string]string{ azure.PublicCloud.Name: "04cc58ec-51ab-4833-ac0d-ce3a7912414b", } userAgent = fmt.Sprintf("packer/%s", version.FormattedVersion()) ) // NOTE(ahmetalpbalkan): Azure Active Directory implements OAuth 2.0 Device Flow // described here: https://tools.ietf.org/html/draft-denniss-oauth-device-flow-00 // Although it has some gotchas, most of the authentication logic is in Azure SDK // for Go helper packages. // // Device auth prints a message to the screen telling the user to click on URL // and approve the app on the browser, meanwhile the client polls the auth API // for a token. Once we have token, we save it locally to a file with proper // permissions and when the token expires (in Azure case typically 1 hour) SDK // will automatically refresh the specified token and will call the refresh // callback function we implement here. This way we will always be storing a // token with a refresh_token saved on the machine.
// wrappedMain is called only when we're wrapped by panicwrap and // returns the exit status to exit with. func wrappedMain() int { // If there is no explicit number of Go threads to use, then set it if os.Getenv("GOMAXPROCS") == "" { runtime.GOMAXPROCS(runtime.NumCPU()) } log.SetOutput(os.Stderr) log.Printf("[INFO] Packer version: %s", version.FormattedVersion()) log.Printf("Packer Target OS/Arch: %s %s", runtime.GOOS, runtime.GOARCH) log.Printf("Built with Go Version: %s", runtime.Version()) // Prepare stdin for plugin usage by switching it to a pipe // But do not switch to pipe in plugin if os.Getenv(plugin.MagicCookieKey) != plugin.MagicCookieValue { setupStdin() } config, err := loadConfig() if err != nil { fmt.Fprintf(os.Stderr, "Error loading configuration: \n\n%s\n", err) return 1 } log.Printf("Packer config: %+v", config) // Fire off the checkpoint. go runCheckpoint(config) cacheDir := os.Getenv("PACKER_CACHE_DIR") if cacheDir == "" { cacheDir = "packer_cache" } cacheDir, err = filepath.Abs(cacheDir) if err != nil { fmt.Fprintf(os.Stderr, "Error preparing cache directory: \n\n%s\n", err) return 1 } log.Printf("Setting cache directory: %s", cacheDir) cache := &packer.FileCache{CacheDir: cacheDir} // Determine if we're in machine-readable mode by mucking around with // the arguments... args, machineReadable := extractMachineReadable(os.Args[1:]) defer plugin.CleanupClients() // Setup the UI if we're being machine-readable var ui packer.Ui = &packer.BasicUi{ Reader: os.Stdin, Writer: os.Stdout, ErrorWriter: os.Stdout, } if machineReadable { ui = &packer.MachineReadableUi{ Writer: os.Stdout, } // Set this so that we don't get colored output in our machine- // readable UI. if err := os.Setenv("PACKER_NO_COLOR", "1"); err != nil { fmt.Fprintf(os.Stderr, "Packer failed to initialize UI: %s\n", err) return 1 } } // Create the CLI meta CommandMeta = &command.Meta{ CoreConfig: &packer.CoreConfig{ Components: packer.ComponentFinder{ Builder: config.LoadBuilder, Hook: config.LoadHook, PostProcessor: config.LoadPostProcessor, Provisioner: config.LoadProvisioner, }, Version: version.Version, }, Cache: cache, Ui: ui, } //setupSignalHandlers(env) cli := &cli.CLI{ Args: args, Commands: Commands, HelpFunc: excludeHelpFunc(Commands, []string{"plugin"}), HelpWriter: os.Stdout, Version: version.Version, } exitCode, err := cli.Run() if err != nil { fmt.Fprintf(os.Stderr, "Error executing CLI: %s\n", err) return 1 } return exitCode }