-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding configs and pubsub implementations for GCP plus additional con…
…text endpoints and service types (#61) * adding support for go-metrics/exp * go-metrics out, kit/metrics in. New config.Metrics type manage providers * removing nop Metrics and using kit/metrics/discard * adding metrics env load, updating examples to use kit/metrics and adding graphite host again as deprecated * [wip] adding initial GCP pubsub implementations * fixing gcp pubsub stop * adding datastore config and server context handlers * adding ctx to pubsub * adding gcp envconfig tags * more context mgmt * using context by default in pubsub * fixing pubsub tests, updating docs * go-metrics out, kit/metrics in. New config.Metrics type manage providers * adding metrics env load, updating examples to use kit/metrics and adding graphite host again as deprecated * [wip] adding initial GCP pubsub implementations * fixing gcp pubsub stop * adding datastore config and server context handlers * adding ctx to pubsub * adding gcp envconfig tags * more context mgmt * using context by default in pubsub * fixing pubsub tests, updating docs * filling in docs for pubsub/gcp * adding tests for pubsub/gcp * adding GCP to config.Config * context should be recreated fresh on each request * updating docs * more doc updates
- Loading branch information
1 parent
53c5ab2
commit 46437ab
Showing
20 changed files
with
661 additions
and
43 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 |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package config | ||
|
||
import ( | ||
"io/ioutil" | ||
"log" | ||
|
||
"golang.org/x/net/context" | ||
"golang.org/x/oauth2" | ||
"golang.org/x/oauth2/google" | ||
"google.golang.org/api/compute/v1" | ||
"google.golang.org/cloud" | ||
) | ||
|
||
type ( | ||
|
||
// GCP holds common Google Cloud Platform credentials. | ||
GCP struct { | ||
ProjectID string `envconfig:"GCP_PROJECT_ID" json:"GCP_PROJECT_ID"` | ||
|
||
// JSONAuthPath points to a file containing a JWT JSON config. | ||
// This is meant to be a fall back for development environments. | ||
JSONAuthPath string `envconfig:"GCP_JSON_AUTH_PATH" json:"GCP_JSON_AUTH_PATH"` | ||
|
||
// Token is a JWT JSON config and may be needed for container | ||
// environments. | ||
Token string `envconfig:"GCP_AUTH_TOKEN" json:"GCP_AUTH_TOKEN"` | ||
} | ||
|
||
// PubSub holds common credentials and config values for | ||
// working with GCP PubSub. | ||
PubSub struct { | ||
GCP | ||
|
||
// For publishing | ||
Topic string `envconfig:"GCP_PUBSUB_TOPIC" json:"GCP_PUBSUB_TOPIC"` | ||
// For subscribing | ||
Subscription string `envconfig:"GCP_PUBSUB_SUBSCRIPTION" json:"GCP_PUBSUB_SUBSCRIPTION"` | ||
} | ||
) | ||
|
||
// LoadGCPFromEnv will attempt to load a GCP config | ||
// from environment variables. | ||
func LoadGCPFromEnv() GCP { | ||
var gcp GCP | ||
LoadEnvConfig(&gcp) | ||
return gcp | ||
} | ||
|
||
// LoadPubSubFromEnv will attempt to load a PubSub config | ||
// from environment variables. | ||
func LoadPubSubFromEnv() PubSub { | ||
var ps PubSub | ||
LoadEnvConfig(&ps) | ||
return ps | ||
} | ||
|
||
// NewContext will check attempt to create a new context from | ||
// a the Token or JSONAuthPath fields if provided, otherwise | ||
// google.DefaultClient will be used. | ||
func (g GCP) NewContext(scopes ...string) (context.Context, error) { | ||
if len(g.Token) > 0 { | ||
return g.contextFromToken(scopes...) | ||
} | ||
|
||
if len(g.JSONAuthPath) > 0 { | ||
return g.contextFromJSON(scopes...) | ||
} | ||
|
||
if len(scopes) == 0 { | ||
scopes = append(scopes, compute.ComputeScope) | ||
} | ||
|
||
client, err := google.DefaultClient(oauth2.NoContext, scopes...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return cloud.NewContext(g.ProjectID, client), nil | ||
} | ||
|
||
func (g GCP) contextFromToken(scopes ...string) (context.Context, error) { | ||
conf, err := google.JWTConfigFromJSON( | ||
[]byte(g.Token), | ||
scopes..., | ||
) | ||
if err != nil { | ||
log.Print("probs with token:", g.Token) | ||
return nil, err | ||
} | ||
|
||
return cloud.NewContext(g.ProjectID, conf.Client(oauth2.NoContext)), nil | ||
} | ||
|
||
func (g GCP) contextFromJSON(scopes ...string) (context.Context, error) { | ||
jsonKey, err := ioutil.ReadFile(g.JSONAuthPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
conf, err := google.JWTConfigFromJSON( | ||
jsonKey, | ||
scopes..., | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return cloud.NewContext(g.ProjectID, conf.Client(oauth2.NoContext)), nil | ||
} |
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
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
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
Oops, something went wrong.