-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(providers): Add support for 1Password using service account toke…
…ns (#378) * feat(providers): Add support for 1Password using service account tokens Signed-off-by: Zois Pagoulatos <[email protected]> * Apply suggestions from code review Signed-off-by: Zois Pagoulatos <[email protected]> --------- Signed-off-by: Zois Pagoulatos <[email protected]> Co-authored-by: yxxhero <[email protected]>
- Loading branch information
Showing
7 changed files
with
157 additions
and
0 deletions.
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,53 @@ | ||
package onepassword | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/1password/onepassword-sdk-go" | ||
|
||
"github.com/helmfile/vals/pkg/api" | ||
) | ||
|
||
type provider struct { | ||
client *onepassword.Client | ||
} | ||
|
||
// New creates a new 1Password provider | ||
func New(cfg api.StaticConfig) *provider { | ||
p := &provider{} | ||
|
||
return p | ||
} | ||
|
||
// Get secret string from 1Password | ||
func (p *provider) GetString(key string) (string, error) { | ||
var err error | ||
|
||
ctx := context.Background() | ||
token := os.Getenv("OP_SERVICE_ACCOUNT_TOKEN") | ||
|
||
client, err := onepassword.NewClient( | ||
ctx, | ||
onepassword.WithServiceAccountToken(token), | ||
onepassword.WithIntegrationInfo("Vals op integration", "v1.0.0"), | ||
) | ||
if err != nil { | ||
return "", fmt.Errorf("storage.NewClient: %v", err) | ||
} | ||
|
||
p.client = client | ||
|
||
prefixedKey := fmt.Sprintf("op://%s", key) | ||
item, err := p.client.Secrets.Resolve(ctx, prefixedKey) | ||
if err != nil { | ||
return "", fmt.Errorf("error retrieving item: %v", err) | ||
} | ||
|
||
return item, nil | ||
} | ||
|
||
func (p *provider) GetStringMap(key string) (map[string]interface{}, error) { | ||
return nil, fmt.Errorf("path fragment is not supported for 1password provider") | ||
} |
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 vals | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
func TestValues_OnePassword_EvalTemplate(t *testing.T) { | ||
// TODO | ||
// 1. Create vault and item for testing and a service account | ||
// op vault create vals-test | ||
// op item create --vault vals-test --title=vals-test [email protected] password=secret --category=login | ||
// op service-account create "Vals Test Service Account" --expires-in 24h --vault vals-test:read_items | ||
|
||
// 2. Set up the new service account token as environment variable: | ||
// export OP_SERVICE_ACCOUNT_TOKEN=ops_xxxxxxxxx | ||
if os.Getenv("SKIP_TESTS") != "" { | ||
t.Skip("Skipping tests") | ||
} | ||
|
||
type testcase struct { | ||
template map[string]interface{} | ||
expected map[string]interface{} | ||
} | ||
vaultName := "vals-test" | ||
itemName := "vals-test" | ||
|
||
testcases := []testcase{ | ||
{ | ||
template: map[string]interface{}{ | ||
"foo": "FOO", | ||
"username": fmt.Sprintf("ref+op://%s/%s/username", vaultName, itemName), | ||
"password": fmt.Sprintf("ref+op://%s/%s/password", vaultName, itemName), | ||
}, | ||
expected: map[string]interface{}{ | ||
"foo": "FOO", | ||
"username": "[email protected]", | ||
"password": "secret", | ||
}, | ||
}, | ||
} | ||
|
||
for i := range testcases { | ||
tc := testcases[i] | ||
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { | ||
vals, err := Eval(tc.template) | ||
if err != nil { | ||
t.Fatalf("%v", err) | ||
} | ||
|
||
diff := cmp.Diff(tc.expected, vals) | ||
if diff != "" { | ||
t.Errorf("unxpected diff: %s", diff) | ||
} | ||
}) | ||
} | ||
} |