-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: added credential_process option to config #17
Open
elyzov
wants to merge
2
commits into
kir4h:master
Choose a base branch
from
elyzov:feat/add-credential-process-support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,60 @@ | ||
package credential | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os/exec" | ||
"strings" | ||
|
||
"github.com/spf13/viper" | ||
) | ||
|
||
const ( | ||
DefaultVersion = "1" | ||
) | ||
|
||
var ( | ||
ErrNoCommandPassed = fmt.Errorf("expected at least a single command to execute") | ||
) | ||
|
||
type ( | ||
Data struct { | ||
Version string | ||
Token string | ||
} | ||
) | ||
|
||
func GetToken(command []string) (string, error) { | ||
if len(command) == 0 { | ||
return "", ErrNoCommandPassed | ||
} | ||
|
||
stdout := new(strings.Builder) | ||
stderr := new(strings.Builder) | ||
|
||
cmd := exec.Command(command[0], command[1:]...) | ||
kir4h marked this conversation as resolved.
Show resolved
Hide resolved
|
||
cmd.Stdout = stdout | ||
cmd.Stderr = stderr | ||
cmd.Env = []string{ | ||
fmt.Sprintf("VAULT_ADDR=%s", viper.GetString("global.address")), | ||
} | ||
Comment on lines
+38
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Worth documenting that this logic will inject |
||
|
||
err := cmd.Run() | ||
if err != nil { | ||
return "", fmt.Errorf("failed to exec process %v: %w, stdout: %s, stderr: %s", command, err, stdout.String(), stderr.String()) | ||
} | ||
|
||
raw := stdout.String() | ||
|
||
var data Data | ||
err = json.Unmarshal([]byte(raw), &data) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to parse process output %s: %w", raw, err) | ||
} | ||
|
||
if data.Version != DefaultVersion { | ||
return "", fmt.Errorf("unexpected credentail process format version %s, expected %s", data.Version, DefaultVersion) | ||
} | ||
|
||
return data.Token, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package credential | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestGetToken_SingleCommand(t *testing.T) { | ||
_, err := GetToken([]string{"test"}) | ||
if !strings.Contains(err.Error(), "exit status 1") { | ||
t.Errorf("GetToken got error = %v, want exit status 1", err) | ||
} | ||
} | ||
kir4h marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
func TestGetToken_NoCommand(t *testing.T) { | ||
_, err := GetToken([]string{}) | ||
if !errors.Is(err, NoCommandPassed) { | ||
t.Errorf("GetToken got error = %v, want NoCommandPassed", err) | ||
} | ||
} | ||
|
||
func TestGetToken_HappyPath(t *testing.T) { | ||
token, err := GetToken([]string{"sh", "-c", `echo '{"Version":"1","Token":"123"}'`}) | ||
if err != nil { | ||
t.Errorf("GetToken got error = %v, want nil", err) | ||
} | ||
if token != "123" { | ||
t.Errorf("GetToken got token = %v, want 123", token) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rather unexport this one as it's not consumed outside of the package
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kir4h I see your concerns, but, as far as I can see, it's not violates any security aspects, but allow to refer this values from the other packages if it's required. On the other hand your proposal restricts the usage of this variables within the single package and will require additional efforts to to provides them for extarnal usage if it's required. Maybe I can't see some other rational in your proposal, so could you please elaborate a bit with your point of view.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not security related, more in line of YAGNI principle (the reason I noticed was the code inspector showing a warning for it)
Also In my view exporting a constant makes think that the constant is used elsewhere, and thus a refactor could be a breaking change where in reality that variable was only consumed internally and thus there is no breaking change.
And if at some point we want to export it it´s just a matter of a small refactor to do so, no big effort there at all.