/
flags.go
185 lines (160 loc) · 4.21 KB
/
flags.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright 2023 Jetpack Technologies Inc and contributors. All rights reserved.
// Use of this source code is governed by the license in the LICENSE file.
package envcli
import (
"fmt"
"os"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"go.jetpack.io/envsec"
"go.jetpack.io/envsec/internal/build"
"go.jetpack.io/envsec/pkg/awsfed"
envsecLib "go.jetpack.io/envsec/pkg/envsec"
"go.jetpack.io/pkg/auth/session"
"go.jetpack.io/pkg/envvar"
"go.jetpack.io/pkg/id"
"go.jetpack.io/typeid"
)
// to be composed into xyzCmdFlags structs
type configFlags struct {
projectID string
orgID string
envName string
}
func (f *configFlags) register(cmd *cobra.Command) {
cmd.PersistentFlags().StringVar(
&f.projectID,
"project-id",
"",
"Project id to namespace secrets by",
)
cmd.PersistentFlags().StringVar(
&f.orgID,
"org-id",
"",
"Organization id to namespace secrets by",
)
cmd.PersistentFlags().StringVar(
&f.envName,
"environment",
"dev",
"Environment name, such as dev or prod",
)
}
func (f *configFlags) validateProjectID(orgID id.OrgID) (string, error) {
if f.projectID != "" {
return f.projectID, nil
}
wd, err := os.Getwd()
if err != nil {
return "", errors.WithStack(err)
}
config, err := (&envsecLib.Envsec{
WorkingDir: wd,
IsDev: build.IsDev,
}).ProjectConfig()
if errors.Is(err, os.ErrNotExist) {
return "", fmt.Errorf(
"project ID not specified. You must run `envsec init` or specify --project-id in this directory",
)
} else if err != nil {
return "", errors.WithStack(err)
}
if config.OrgID != orgID {
// Validate that the project ID belongs to the org ID
return "", errors.Errorf(
"Project ID %s does not belong to organization %s",
config.ProjectID,
orgID,
)
}
return config.ProjectID.String(), nil
}
type CmdConfig struct {
Store envsec.Store
EnvID envsec.EnvID
EnvNames []string
}
func (f *configFlags) genConfig(cmd *cobra.Command) (*CmdConfig, error) {
if bootstrappedConfig != nil {
return bootstrappedConfig, nil
}
ctx := cmd.Context()
var tok *session.Token
var err error
if f.orgID == "" {
client, err := newAuthClient()
if err != nil {
return nil, err
}
wd, err := os.Getwd()
if err != nil {
return nil, errors.WithStack(err)
}
// This is a bit of a temporary hack. Ideally get,set,list logic moves
// into envsec lib and it will choose correct credentials based on org.
// It will also take flags that override project and organization.
project, _ := defaultEnvsec(cmd, wd).ProjectConfig()
if project != nil {
tok, err = client.LoginFlowIfNeededForOrg(ctx, project.OrgID.String())
} else {
tok, err = client.LoginFlowIfNeeded(ctx)
}
if err != nil {
return nil, fmt.Errorf(
"error: %w. To use envsec you must log in (`envsec auth login`) or specify --project-id and --org-id",
err,
)
}
}
var store envsec.Store
if envvar.Bool("ENVSEC_USE_AWS_STORE") {
// Temporary hack to enable the AWS store
ssmConfig, err := awsfed.GenSSMConfigFromToken(ctx, tok, true /*useCache*/)
if err != nil {
return nil, errors.WithStack(err)
}
store, err = envsec.NewStore(ctx, ssmConfig)
if err != nil {
return nil, errors.WithStack(err)
}
} else {
store, err = envsec.NewStore(ctx, envsec.NewJetpackAPIConfig(tok))
if err != nil {
return nil, errors.WithStack(err)
}
}
if err != nil {
return nil, errors.WithStack(err)
}
if tok != nil && f.orgID == "" {
f.orgID = tok.IDClaims().OrgID
}
orgID, err := typeid.Parse[id.OrgID](f.orgID)
if err != nil {
return nil, errors.WithStack(err)
}
projectID, err := f.validateProjectID(orgID)
if err != nil {
return nil, errors.WithStack(err)
}
envid, err := envsec.NewEnvID(projectID, f.orgID, f.envName)
if err != nil {
return nil, errors.WithStack(err)
}
envNames := []string{"dev", "prod", "preview"}
if cmd.Flags().Changed(environmentFlagName) {
envNames = []string{envid.EnvName}
}
return &CmdConfig{
Store: store,
EnvID: envid,
EnvNames: envNames,
}, nil
}
var bootstrappedConfig *CmdConfig
// BootstrapConfig is used to set the config for all commands that use genConfig
// Useful for using envsec programmatically.
func BootstrapConfig(cmdConfig *CmdConfig) {
bootstrappedConfig = cmdConfig
}