Skip to content
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

fix: fix op cli always generating passwords #127

Merged
merged 2 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions onepassword/cli/op.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,9 @@ func (op *OP) create(ctx context.Context, item *onepassword.Item, vaultUuid stri
args := []opArg{p("item"), p("create"), p("-")}
// 'op item create' command doesn't support generating passwords when using templates
// therefore need to use --generate-password flag to set it
if pf := passwordField(item); pf != nil {
recipeStr := "letters,digits,32"
if pf.Recipe != nil {
recipeStr = passwordRecipeToString(pf.Recipe)
}
args = append(args, f("generate-password", recipeStr))
recipe := passwordRecipe(item)
if recipe != "" {
args = append(args, f("generate-password", recipe))
}

err = op.execJson(ctx, &res, payload, args...)
Expand Down
7 changes: 7 additions & 0 deletions onepassword/cli/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ func passwordField(item *onepassword.Item) *onepassword.ItemField {
return nil
}

func passwordRecipe(item *onepassword.Item) string {
if pf := passwordField(item); pf != nil {
return passwordRecipeToString(pf.Recipe)
}
return ""
}

func passwordRecipeToString(recipe *onepassword.GeneratorRecipe) string {
str := ""
if recipe != nil {
Expand Down
49 changes: 49 additions & 0 deletions onepassword/cli/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,55 @@ func TestPasswordField(t *testing.T) {
}
}

func TestPasswordRecipeExtraction(t *testing.T) {
tests := map[string]struct {
item *onepassword.Item
expectedString string
}{
"should return empty string if item has no fields": {
item: &onepassword.Item{},
expectedString: "",
},
"should return empty string if no password field": {
item: &onepassword.Item{
Fields: []*onepassword.ItemField{
{Purpose: onepassword.FieldPurposeNotes},
},
},
expectedString: "",
},
"should return empty string if no password recipe": {
item: &onepassword.Item{
Fields: []*onepassword.ItemField{
{ID: "username", Purpose: onepassword.FieldPurposeUsername},
{ID: "password", Purpose: onepassword.FieldPurposePassword},
},
},
expectedString: "",
},
"should return recipe string": {
item: &onepassword.Item{
Fields: []*onepassword.ItemField{
{ID: "username", Purpose: onepassword.FieldPurposeUsername},
{ID: "password", Purpose: onepassword.FieldPurposePassword, Recipe: &onepassword.GeneratorRecipe{
Length: 30,
}},
},
},
expectedString: "30",
},
}

for description, test := range tests {
t.Run(description, func(t *testing.T) {
actualString := passwordRecipe(test.item)
if actualString != test.expectedString {
t.Errorf("Unexpected password recipe string. Expected \"%s\", but got \"%s\"", test.expectedString, actualString)
}
})
}
}

func TestPasswordRecipeToString(t *testing.T) {
tests := map[string]struct {
recipe *onepassword.GeneratorRecipe
Expand Down
Loading