Skip to content

Commit e2a77af

Browse files
szhGitHub Enterprise
authored and
GitHub Enterprise
committed
Merge pull request #19 from Conjur-Enterprise/pr-255
CNJR-7654: Summon Community PR: bring back --up flag
2 parents 64ade28 + ecce0b9 commit e2a77af

File tree

3 files changed

+81
-24
lines changed

3 files changed

+81
-24
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66

77
## [Unreleased]
88

9+
## [0.10.2] - 2025-01-10
10+
11+
### Changed
12+
- Fix: bring back the --up flag [#254](https://github.com/cyberark/summon/issues/254), [#255](https://github.com/cyberark/summon/pull/255).
13+
914
## [0.10.1] - 2024-08-14
1015

1116
### Changed

pkg/summon/summon.go

+11
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ func RunSubprocess(sc *SubprocessConfig) (int, error) {
4545

4646
subs := convertSubsToMap(sc.Subs)
4747

48+
if sc.RecurseUp {
49+
currentDir, err := os.Getwd()
50+
if err != nil {
51+
return 0, err
52+
}
53+
sc.Filepath, err = findInParentTree(sc.Filepath, currentDir)
54+
if err != nil {
55+
return 0, err
56+
}
57+
}
58+
4859
switch sc.YamlInline {
4960
case "":
5061
secrets, err = secretsyml.ParseFromFile(sc.Filepath, sc.Environment, subs)

pkg/summon/summon_test.go

+65-24
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,32 @@ func TestRunSubprocess(t *testing.T) {
4848

4949
assert.Equal(t, expectedValue, string(content))
5050
})
51+
52+
t.Run("Finds secrets file in a directory above the working directory", func(t *testing.T) {
53+
var err error
54+
topDir := t.TempDir()
55+
56+
fileAbovePath := filepath.Join(topDir, "secrets.yml")
57+
_, err = os.Create(fileAbovePath)
58+
assert.NoError(t, err)
59+
60+
// Create a downwards directory hierarchy, starting from topDir, and
61+
// chdir to it.
62+
downDir := filepath.Join(topDir, "dir1", "dir2", "dir3")
63+
err = os.MkdirAll(downDir, 0o700)
64+
assert.NoError(t, err)
65+
restore := chdir(t, downDir)
66+
t.Cleanup(restore)
67+
68+
code, err := RunSubprocess(&SubprocessConfig{
69+
Args: []string{"true"},
70+
RecurseUp: true,
71+
Filepath: "secrets.yml",
72+
})
73+
74+
assert.NoError(t, err)
75+
assert.Equal(t, 0, code)
76+
})
5177
}
5278

5379
func TestHandleResultsFromProvider(t *testing.T) {
@@ -244,6 +270,7 @@ func TestDefaultVariableResolutionWithValue(t *testing.T) {
244270
assert.Equal(t, expectedValue, string(content))
245271
})
246272
}
273+
247274
func TestConvertSubsToMap(t *testing.T) {
248275
t.Run("Substitutions are returned as a map used later for interpolation", func(t *testing.T) {
249276
input := []string{
@@ -318,12 +345,10 @@ func TestLocateFileRecurseUp(t *testing.T) {
318345
filename := "test.txt"
319346

320347
t.Run("Finds file in current working directory", func(t *testing.T) {
321-
topDir, err := os.MkdirTemp("", "summon")
322-
assert.NoError(t, err)
323-
defer os.RemoveAll(topDir)
348+
topDir := t.TempDir()
324349

325350
localFilePath := filepath.Join(topDir, filename)
326-
_, err = os.Create(localFilePath)
351+
_, err := os.Create(localFilePath)
327352
assert.NoError(t, err)
328353

329354
gotPath, err := findInParentTree(filename, topDir)
@@ -332,62 +357,54 @@ func TestLocateFileRecurseUp(t *testing.T) {
332357
assert.Equal(t, localFilePath, gotPath)
333358
})
334359

335-
t.Run("Finds file in a higher working directory", func(t *testing.T) {
336-
topDir, err := os.MkdirTemp("", "summon")
337-
assert.NoError(t, err)
338-
defer os.RemoveAll(topDir)
360+
t.Run("Finds file in a directory above the working directory", func(t *testing.T) {
361+
topDir := t.TempDir()
339362

340-
higherFilePath := filepath.Join(topDir, filename)
341-
_, err = os.Create(higherFilePath)
363+
fileAbovePath := filepath.Join(topDir, filename)
364+
_, err := os.Create(fileAbovePath)
342365
assert.NoError(t, err)
343366

344367
// Create a downwards directory hierarchy, starting from topDir
345368
downDir := filepath.Join(topDir, "dir1", "dir2", "dir3")
346-
err = os.MkdirAll(downDir, 0700)
369+
err = os.MkdirAll(downDir, 0o700)
347370
assert.NoError(t, err)
348371

349372
gotPath, err := findInParentTree(filename, downDir)
350373
assert.NoError(t, err)
351374

352-
assert.Equal(t, higherFilePath, gotPath)
375+
assert.Equal(t, fileAbovePath, gotPath)
353376
})
354377

355378
t.Run("returns a friendly error if file not found", func(t *testing.T) {
356-
topDir, err := os.MkdirTemp("", "summon")
357-
assert.NoError(t, err)
358-
defer os.RemoveAll(topDir)
379+
topDir := t.TempDir()
359380

360381
// A unlikely to exist file name
361382
nonExistingFileName := strconv.FormatInt(time.Now().Unix(), 10)
362383
wantErrMsg := fmt.Sprintf(
363384
"unable to locate file specified (%s): reached root of file system",
364385
nonExistingFileName)
365386

366-
_, err = findInParentTree(nonExistingFileName, topDir)
387+
_, err := findInParentTree(nonExistingFileName, topDir)
367388
assert.EqualError(t, err, wantErrMsg)
368389
})
369390

370391
t.Run("returns a friendly error if file is an absolute path", func(t *testing.T) {
371-
topDir, err := os.MkdirTemp("", "summon")
372-
assert.NoError(t, err)
373-
defer os.RemoveAll(topDir)
392+
topDir := t.TempDir()
374393

375394
absFileName := "/foo/bar/baz"
376395
wantErrMsg := "file specified (/foo/bar/baz) is an absolute path: will not recurse up"
377396

378-
_, err = findInParentTree(absFileName, topDir)
397+
_, err := findInParentTree(absFileName, topDir)
379398
assert.EqualError(t, err, wantErrMsg)
380399
})
381400

382401
t.Run("returns a friendly error in unexpected circumstances (100% coverage)", func(t *testing.T) {
383-
topDir, err := os.MkdirTemp("", "summon")
384-
assert.NoError(t, err)
385-
defer os.RemoveAll(topDir)
402+
topDir := t.TempDir()
386403

387404
fileNameWithNulByte := "pizza\x00margherita"
388405
wantErrMsg := "unable to locate file specified (pizza\x00margherita): stat"
389406

390-
_, err = findInParentTree(fileNameWithNulByte, topDir)
407+
_, err := findInParentTree(fileNameWithNulByte, topDir)
391408
assert.Contains(t, err.Error(), wantErrMsg)
392409
})
393410
}
@@ -412,6 +429,7 @@ func TestReturnStatusOfError(t *testing.T) {
412429
assert.Equal(t, expected, err)
413430
})
414431
}
432+
415433
func TestNonInteractiveProviderFallback(t *testing.T) {
416434
secrets := secretsyml.SecretsMap{
417435
"key1": secretsyml.SecretSpec{Path: "path1"},
@@ -433,3 +451,26 @@ func TestNonInteractiveProviderFallback(t *testing.T) {
433451
assert.Nil(t, result.Error)
434452
}
435453
}
454+
455+
// chdir changes the current working directory to the named directory and
456+
// returns a function that, when called, restores the original working
457+
// directory.
458+
//
459+
// Courtesy of https://github.com/golang/go/issues/45182
460+
// Can be replaced by https://pkg.go.dev/testing@master#T.Chdir
461+
// when Go 1.24 is out (2025-02).
462+
func chdir(t *testing.T, dir string) func() {
463+
wd, err := os.Getwd()
464+
if err != nil {
465+
t.Fatalf("chdir %s: %v", dir, err)
466+
}
467+
if err := os.Chdir(dir); err != nil {
468+
t.Fatal(err)
469+
}
470+
471+
return func() {
472+
if err := os.Chdir(wd); err != nil {
473+
t.Fatalf("restoring working directory: %v", err)
474+
}
475+
}
476+
}

0 commit comments

Comments
 (0)