-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
using a build tag so we can use GAE std with auth/gcp
- Loading branch information
1 parent
ba3c663
commit 9aa1786
Showing
4 changed files
with
45 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
// +build !appengine | ||
|
||
package gcp | ||
|
||
import ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package gcp | ||
|
||
import ( | ||
"context" | ||
"io/ioutil" | ||
"net/http" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
// GetDefaultEmail is a helper method for users on GCE or the 2nd generation GAE | ||
// environment. | ||
func GetDefaultEmail(ctx context.Context, addr string, hc *http.Client) (string, error) { | ||
email, err := metadataGet(ctx, addr, hc, "instance/service-accounts/default/email") | ||
return email, errors.Wrap(err, "unable to get default email from metadata") | ||
} | ||
|
||
func metadataGet(ctx context.Context, addr string, hc *http.Client, suffix string) (string, error) { | ||
if addr == "" { | ||
addr = "http://metadata/computeMetadata/v1/" | ||
} | ||
req, err := http.NewRequest(http.MethodGet, addr+suffix, nil) | ||
if err != nil { | ||
return "", errors.Wrap(err, "unable to create metadata request") | ||
} | ||
req.Header.Set("Metadata-Flavor", "Google") | ||
|
||
resp, err := hc.Do(req) | ||
if err != nil { | ||
return "", errors.Wrap(err, "unable to send request to metadata") | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return "", errors.Errorf("metadata service returned a non-200 response: %d", | ||
resp.StatusCode) | ||
} | ||
|
||
tkn, err := ioutil.ReadAll(resp.Body) | ||
return string(tkn), errors.Wrap(err, "unable to read metadata response") | ||
} |