-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(providers): first implementation of a 1password connect provider (…
…#164) * feat(providers): first implementation of a 1password connect provider Signed-off-by: Fabian Jucker <[email protected]> * style: remove newline Signed-off-by: Fabian Jucker <[email protected]> * test: add basic integration test Signed-off-by: Fabian Jucker <[email protected]> --------- Signed-off-by: Fabian Jucker <[email protected]>
- Loading branch information
Showing
8 changed files
with
218 additions
and
19 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,90 @@ | ||
package onepasswordconnect | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/1Password/connect-sdk-go/connect" | ||
"gopkg.in/yaml.v3" | ||
|
||
"github.com/helmfile/vals/pkg/api" | ||
) | ||
|
||
type provider struct { | ||
client connect.Client | ||
} | ||
|
||
// New creates a new 1Password Connect provider | ||
func New(cfg api.StaticConfig) *provider { | ||
p := &provider{} | ||
|
||
return p | ||
} | ||
|
||
// Get secret string from 1Password Connect | ||
func (p *provider) GetString(key string) (string, error) { | ||
var err error | ||
|
||
splits := strings.Split(key, "/") | ||
if len(splits) < 2 { | ||
return "", fmt.Errorf("invalid URI: %v", errors.New("vault or item missing")) | ||
} | ||
|
||
client, err := connect.NewClientFromEnvironment() | ||
if err != nil { | ||
return "", fmt.Errorf("storage.NewClient: %v", err) | ||
} | ||
|
||
p.client = client | ||
|
||
item, err := client.GetItem(splits[1], splits[0]) | ||
if err != nil { | ||
return "", fmt.Errorf("error retrieving item: %v", err) | ||
} | ||
|
||
var data = make(map[string]string) | ||
// fill map with all possible ID/Label combinations for value | ||
for _, f := range item.Fields { | ||
data[f.ID] = f.Value | ||
// if no section on field (default fields on some item types) use value directly | ||
if f.Section == nil { | ||
data[f.Label] = f.Value | ||
} else { | ||
if f.Section.Label != "" { | ||
var key = strings.Join([]string{f.Section.Label, f.Label}, ".") | ||
data[key] = f.Value | ||
key = strings.Join([]string{f.Section.Label, f.ID}, ".") | ||
data[key] = f.Value | ||
} | ||
key = strings.Join([]string{f.Section.ID, f.Label}, ".") | ||
data[key] = f.Value | ||
key = strings.Join([]string{f.Section.ID, f.ID}, ".") | ||
data[key] = f.Value | ||
} | ||
} | ||
var yamlData []byte | ||
yamlData, err = yaml.Marshal(data) | ||
if err != nil { | ||
return "", fmt.Errorf("yaml.Marshal: %v", err) | ||
} | ||
|
||
return (string)(yamlData), nil | ||
} | ||
|
||
// Convert yaml to map interface and return the requested keys | ||
func (p *provider) GetStringMap(key string) (map[string]interface{}, error) { | ||
yamlData, err := p.GetString(key) | ||
if err != nil { | ||
fmt.Println(err) | ||
return nil, err | ||
} | ||
|
||
m := map[string]interface{}{} | ||
|
||
if err := yaml.Unmarshal([]byte(yamlData), &m); err != nil { | ||
return nil, err | ||
} | ||
|
||
return m, 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
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,59 @@ | ||
package vals | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
func TestValues_OnePasswordConnect_EvalTemplate(t *testing.T) { | ||
// TODO | ||
// create vault and item for testing | ||
// op vault create vals-test | ||
// op item create --vault vals-test --title=vals-test [email protected] password=secret --category=login | ||
|
||
// Pre-requisite: | ||
// Setup 1Password connect service with access to `vals-test` vault: https://developer.1password.com/docs/connect/ | ||
|
||
// set up service principal credentials in the environment: | ||
// "OP_CONNECT_TOKEN": "...", | ||
// "OP_CONNECT_HOST": "...", | ||
|
||
type testcase struct { | ||
template map[string]interface{} | ||
expected map[string]interface{} | ||
} | ||
vaultLabel := "vals-test" | ||
itemLabel := "vals-test" | ||
|
||
testcases := []testcase{ | ||
{ | ||
template: map[string]interface{}{ | ||
"foo": "FOO", | ||
"username": fmt.Sprintf("ref+onepasswordconnect://%s/%s#/username", vaultLabel, itemLabel), | ||
"password": fmt.Sprintf("ref+onepasswordconnect://%s/%s#/password", vaultLabel, itemLabel), | ||
}, | ||
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) | ||
} | ||
}) | ||
} | ||
} |