-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathexport.go
More file actions
316 lines (274 loc) · 10.3 KB
/
export.go
File metadata and controls
316 lines (274 loc) · 10.3 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
Copyright (c) 2023 Infisical Inc.
*/
package cmd
import (
"encoding/csv"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/Infisical/infisical-merge/packages/models"
"github.com/Infisical/infisical-merge/packages/util"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
)
const (
FormatDotenv string = "dotenv"
FormatJson string = "json"
FormatCSV string = "csv"
FormatYaml string = "yaml"
FormatDotEnvExport string = "dotenv-export"
)
// exportCmd represents the export command
var exportCmd = &cobra.Command{
Use: "export",
Short: "Used to export environment variables to a file",
DisableFlagsInUseLine: true,
Example: "infisical export --env=prod --format=json > secrets.json\ninfisical export --env=prod --format=json --output-file=secrets.json",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
token, projectConfig := util.GetTokenAndProjectConfigFromCommand(cmd)
shouldExpandSecrets := util.GetBooleanArgument(cmd, "expand", "Unable to parse flag --expand")
includeImports := util.GetBooleanArgument(cmd, "include-imports", "Unable to parse flag --include-imports")
format := util.GetStringArgument(cmd, "format", "Unable to parse flag --format")
templatePath := util.GetStringArgument(cmd, "template", "Unable to parse flag --template")
secretOverriding := util.GetBooleanArgument(cmd, "secret-overriding", "Unable to parse flag --secret-overriding")
outputFile := util.GetStringArgument(cmd, "output-file", "Unable to parse flag --output-file")
request := models.GetAllSecretsParameters{
Environment: projectConfig.Environment,
TagSlugs: projectConfig.TagSlugs,
WorkspaceId: projectConfig.WorkspaceId,
SecretsPath: projectConfig.SecretsPath,
IncludeImport: includeImports,
ExpandSecretReferences: shouldExpandSecrets,
}
if token != nil && token.Type == util.SERVICE_TOKEN_IDENTIFIER {
request.InfisicalToken = token.Token
} else if token != nil && token.Type == util.UNIVERSAL_AUTH_TOKEN_IDENTIFIER {
request.UniversalAuthAccessToken = token.Token
}
if templatePath != "" {
sigChan := make(chan os.Signal, 1)
dynamicSecretLeases := NewDynamicSecretLeaseManager(sigChan)
newEtag := ""
accessToken := ""
if token != nil {
accessToken = token.Token
} else {
log.Debug().Msg("GetAllEnvironmentVariables: Trying to fetch secrets using logged in details")
loggedInUserDetails, err := util.GetCurrentLoggedInUserDetails(true)
if err != nil {
util.HandleError(err)
}
accessToken = loggedInUserDetails.UserCredentials.JTWToken
}
processedTemplate, err := ProcessTemplate(1, templatePath, nil, accessToken, "", &newEtag, dynamicSecretLeases)
if err != nil {
util.HandleError(err)
}
fmt.Print(processedTemplate.String())
return
}
secrets, err := util.GetAllEnvironmentVariables(request)
if err != nil {
util.HandleError(err, "Unable to fetch secrets")
}
if secretOverriding {
secrets = util.OverrideSecrets(secrets, util.SECRET_TYPE_PERSONAL)
} else {
secrets = util.OverrideSecrets(secrets, util.SECRET_TYPE_SHARED)
}
var output string
secrets = util.FilterSecretsByTag(secrets, projectConfig.TagSlugs)
secrets = util.SortSecretsByKeys(secrets)
output, err = formatEnvs(secrets, format)
if err != nil {
util.HandleError(err)
}
// Handle output file logic - only save to file if --output-file is specified
if outputFile != "" {
finalPath, err := resolveOutputPath(outputFile, format)
if err != nil {
util.HandleError(err, "Unable to resolve output path")
}
err = writeToFile(finalPath, output, 0644)
if err != nil {
util.HandleError(err, "Failed to write output to file")
}
fmt.Printf("Successfully exported secrets to: %s\n", finalPath)
} else {
// Original behavior - print to stdout when no output file specified
fmt.Print(output)
}
// Telemetry.CaptureEvent("cli-command:export", posthog.NewProperties().Set("secretsCount", len(secrets)).Set("version", util.CLI_VERSION))
},
}
// resolveOutputPath determines the final output path based on the provided path and format
func resolveOutputPath(outputFile, format string) (string, error) {
// Expand ~ to home directory if present
if strings.HasPrefix(outputFile, "~") {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("failed to resolve home directory: %w", err)
}
outputFile = strings.Replace(outputFile, "~", homeDir, 1)
}
// Get absolute path to handle relative paths consistently
absPath, err := filepath.Abs(outputFile)
if err != nil {
return "", fmt.Errorf("failed to resolve absolute path: %w", err)
}
// Check if the path is a directory
if info, err := os.Stat(absPath); err == nil && info.IsDir() {
// If it's a directory, append the default filename
defaultFilename := getDefaultFilename(format)
return filepath.Join(absPath, defaultFilename), nil
} else if os.IsNotExist(err) {
// Path doesn't exist, check if it looks like a directory (ends with /)
if strings.HasSuffix(absPath, string(filepath.Separator)) {
// Treat as directory, create it and add default filename
err := os.MkdirAll(absPath, 0755)
if err != nil {
return "", fmt.Errorf("failed to create directory %s: %w", absPath, err)
}
defaultFilename := getDefaultFilename(format)
return filepath.Join(absPath, defaultFilename), nil
}
// Ensure the parent directory exists
parentDir := filepath.Dir(absPath)
if _, err := os.Stat(parentDir); os.IsNotExist(err) {
err := os.MkdirAll(parentDir, 0755)
if err != nil {
return "", fmt.Errorf("failed to create parent directory %s: %w", parentDir, err)
}
}
// If no extension provided, add default extension based on format
if filepath.Ext(absPath) == "" {
ext := getDefaultExtension(format)
absPath += ext
}
}
return absPath, nil
}
// getDefaultFilename returns the default filename based on the format
func getDefaultFilename(format string) string {
switch strings.ToLower(format) {
case FormatJson:
return "secrets.json"
case FormatCSV:
return "secrets.csv"
case FormatYaml:
return "secrets.yaml"
case FormatDotEnvExport:
return ".env"
case FormatDotenv:
return ".env"
default:
return ".env"
}
}
// getDefaultExtension returns the default file extension based on the format
func getDefaultExtension(format string) string {
switch strings.ToLower(format) {
case FormatJson:
return ".json"
case FormatCSV:
return ".csv"
case FormatYaml:
return ".yaml"
case FormatDotEnvExport:
return ".env"
case FormatDotenv:
return ".env"
default:
return ".env"
}
}
func init() {
rootCmd.AddCommand(exportCmd)
exportCmd.Flags().StringP("env", "e", "dev", "Set the environment (dev, prod, etc.) from which your secrets should be pulled from")
exportCmd.Flags().Bool("expand", true, "Parse shell parameter expansions in your secrets")
exportCmd.Flags().StringP("format", "f", "dotenv", "Set the format of the output file (dotenv, json, csv)")
exportCmd.Flags().Bool("secret-overriding", true, "Prioritizes personal secrets, if any, with the same name over shared secrets")
exportCmd.Flags().Bool("include-imports", true, "Imported linked secrets")
exportCmd.Flags().String("token", "", "Fetch secrets using service token or machine identity access token")
exportCmd.Flags().StringP("tags", "t", "", "filter secrets by tag slugs")
exportCmd.Flags().String("projectId", "", "manually set the projectId to export secrets from")
exportCmd.Flags().String("path", "/", "get secrets within a folder path")
exportCmd.Flags().String("template", "", "The path to the template file used to render secrets")
exportCmd.Flags().StringP("output-file", "o", "", "The path to write the output file to. Can be a full file path, directory, or filename. If not specified, output will be printed to stdout")
}
// Format according to the format flag
func formatEnvs(envs []models.SingleEnvironmentVariable, format string) (string, error) {
switch strings.ToLower(format) {
case FormatDotenv:
return formatAsDotEnv(envs), nil
case FormatDotEnvExport:
return formatAsDotEnvExport(envs), nil
case FormatJson:
return formatAsJson(envs), nil
case FormatCSV:
return formatAsCSV(envs), nil
case FormatYaml:
return formatAsYaml(envs)
default:
return "", fmt.Errorf("invalid format type: %s. Available format types are [%s]", format, []string{FormatDotenv, FormatJson, FormatCSV, FormatYaml, FormatDotEnvExport})
}
}
// Format environment variables as a CSV file
func formatAsCSV(envs []models.SingleEnvironmentVariable) string {
csvString := &strings.Builder{}
writer := csv.NewWriter(csvString)
writer.Write([]string{"Key", "Value"})
for _, env := range envs {
writer.Write([]string{env.Key, escapeNewLinesIfRequired(env)})
}
writer.Flush()
return csvString.String()
}
// Format environment variables as a dotenv file
func formatAsDotEnv(envs []models.SingleEnvironmentVariable) string {
var dotenv string
for _, env := range envs {
dotenv += fmt.Sprintf("%s='%s'\n", env.Key, escapeNewLinesIfRequired(env))
}
return dotenv
}
// Format environment variables as a dotenv file with export at the beginning
func formatAsDotEnvExport(envs []models.SingleEnvironmentVariable) string {
var dotenv string
for _, env := range envs {
dotenv += fmt.Sprintf("export %s='%s'\n", env.Key, escapeNewLinesIfRequired(env))
}
return dotenv
}
func formatAsYaml(envs []models.SingleEnvironmentVariable) (string, error) {
m := make(map[string]string)
for _, env := range envs {
m[env.Key] = escapeNewLinesIfRequired(env)
}
yamlBytes, err := yaml.Marshal(m)
if err != nil {
return "", fmt.Errorf("failed to format environment variables as YAML: %w", err)
}
return string(yamlBytes), nil
}
// Format environment variables as a JSON file
func formatAsJson(envs []models.SingleEnvironmentVariable) string {
// Dump as a json array
json, err := json.Marshal(envs)
if err != nil {
log.Err(err).Msgf("Unable to marshal environment variables to JSON")
return ""
}
return string(json)
}
func escapeNewLinesIfRequired(env models.SingleEnvironmentVariable) string {
if env.IsMultilineEncodingEnabled() && strings.ContainsRune(env.Value, '\n') {
return strings.ReplaceAll(env.Value, "\n", "\\n")
}
return env.Value
}