|
| 1 | +package secret |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "cloud.google.com/go/compute/metadata" |
| 10 | + secretmanager "cloud.google.com/go/secretmanager/apiv1" |
| 11 | + secretmanagerpb "google.golang.org/genproto/googleapis/cloud/secretmanager/v1" |
| 12 | +) |
| 13 | + |
| 14 | +// FlagResolver contains the dependencies necessary to resolve a Secret flag. |
| 15 | +type FlagResolver struct { |
| 16 | + Context context.Context |
| 17 | + Client secretClient |
| 18 | + DefaultProjectID string |
| 19 | +} |
| 20 | + |
| 21 | +// Flag declares a string flag on set that will be resolved using r. |
| 22 | +func (r *FlagResolver) Flag(set *flag.FlagSet, name string, usage string) *string { |
| 23 | + var value string |
| 24 | + suffixedUsage := usage + " [ specify `secret:[project name/]<secret name>` to read from Secret Manager ]" |
| 25 | + set.Func(name, suffixedUsage, func(flagValue string) error { |
| 26 | + if r.Client == nil || r.Context == nil { |
| 27 | + return fmt.Errorf("secret resolver was not initialized") |
| 28 | + } |
| 29 | + if !strings.HasPrefix(flagValue, "secret:") { |
| 30 | + value = flagValue |
| 31 | + return nil |
| 32 | + } |
| 33 | + |
| 34 | + secretName := strings.TrimPrefix(flagValue, "secret:") |
| 35 | + projectID := r.DefaultProjectID |
| 36 | + if parts := strings.SplitN(secretName, "/", 2); len(parts) == 2 { |
| 37 | + projectID, secretName = parts[0], parts[1] |
| 38 | + } |
| 39 | + if projectID == "" { |
| 40 | + return fmt.Errorf("missing project ID: none specified in %q, and no default set (not on GCP?)", secretName) |
| 41 | + } |
| 42 | + r, err := r.Client.AccessSecretVersion(r.Context, &secretmanagerpb.AccessSecretVersionRequest{ |
| 43 | + Name: buildNamePath(projectID, secretName, "latest"), |
| 44 | + }) |
| 45 | + if err != nil { |
| 46 | + return fmt.Errorf("reading secret %q from project %v failed: %v", secretName, projectID, err) |
| 47 | + } |
| 48 | + value = string(r.Payload.GetData()) |
| 49 | + return nil |
| 50 | + }) |
| 51 | + return &value |
| 52 | +} |
| 53 | + |
| 54 | +// DefaultResolver is the FlagResolver used by the convenience functions. |
| 55 | +var DefaultResolver FlagResolver |
| 56 | + |
| 57 | +// Flag declares a string flag on flag.CommandLine that supports Secret Manager |
| 58 | +// resolution for values like "secret:<secret name>". InitFlagSupport must be |
| 59 | +// called before flag.Parse. |
| 60 | +func Flag(name string, usage string) *string { |
| 61 | + return DefaultResolver.Flag(flag.CommandLine, name, usage) |
| 62 | +} |
| 63 | + |
| 64 | +// InitFlagSupport initializes the dependencies for flags declared with Flag. |
| 65 | +func InitFlagSupport(ctx context.Context) error { |
| 66 | + client, err := secretmanager.NewClient(ctx) |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + DefaultResolver = FlagResolver{ |
| 71 | + Context: ctx, |
| 72 | + Client: client, |
| 73 | + } |
| 74 | + if metadata.OnGCE() { |
| 75 | + projectID, err := metadata.ProjectID() |
| 76 | + if err != nil { |
| 77 | + return err |
| 78 | + } |
| 79 | + DefaultResolver.DefaultProjectID = projectID |
| 80 | + } |
| 81 | + |
| 82 | + return nil |
| 83 | +} |
0 commit comments