Skip to content

Commit

Permalink
add secret engine to ID
Browse files Browse the repository at this point in the history
  • Loading branch information
mandelsoft committed Nov 2, 2023
1 parent 8e54109 commit a94128e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 15 deletions.
2 changes: 2 additions & 0 deletions docs/reference/ocm_credential-handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ The following credential consumer types are used/supported:
- <code>scheme</code>: (optional) URL scheme
- <code>port</code>: (optional) server port
- <code>namespace</code>: vault namespace
- <code>secretEngine</code>: secret engine
- <code>pathprefix</code>: path prefix for secret


Expand Down Expand Up @@ -259,6 +260,7 @@ behaviours are described in the following list:
- <code>scheme</code>: (optional) URL scheme
- <code>port</code>: (optional) server port
- <code>namespace</code>: vault namespace
- <code>secretEngine</code>: secret engine
- <code>pathprefix</code>: path prefix for secret


Expand Down
1 change: 1 addition & 0 deletions docs/reference/ocm_get_credentials.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Matchers exist for the following usage contexts or consumer types:
- <code>scheme</code>: (optional) URL scheme
- <code>port</code>: (optional) server port
- <code>namespace</code>: vault namespace
- <code>secretEngine</code>: secret engine
- <code>pathprefix</code>: path prefix for secret


Expand Down
30 changes: 18 additions & 12 deletions pkg/contexts/credentials/repositories/vault/identity/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package identity
import (
"net"
"net/url"
"path"
"strings"

"github.com/open-component-model/ocm/pkg/contexts/credentials/cpi"
Expand All @@ -20,11 +19,12 @@ const CONSUMER_TYPE = "HashiCorpVault"

// identity properties.
const (
ID_HOSTNAME = hostpath.ID_HOSTNAME
ID_SCHEMA = hostpath.ID_SCHEME
ID_PORT = hostpath.ID_PORT
ID_PATHPREFIX = hostpath.ID_PATHPREFIX
ID_NAMESPACE = "namespace"
ID_HOSTNAME = hostpath.ID_HOSTNAME
ID_SCHEMA = hostpath.ID_SCHEME
ID_PORT = hostpath.ID_PORT
ID_PATHPREFIX = hostpath.ID_PATHPREFIX
ID_SECRETENGINE = "secretEngine"
ID_NAMESPACE = "namespace"
)

// credential properties.
Expand All @@ -46,6 +46,9 @@ func IdentityMatcher(request, cur, id cpi.ConsumerIdentity) bool {
if id[ID_NAMESPACE] != request[ID_NAMESPACE] {
return false
}
if id[ID_SECRETENGINE] != "" && id[ID_SECRETENGINE] != request[ID_SECRETENGINE] {
return false
}
return identityMatcher(request, cur, id)
}

Expand All @@ -62,6 +65,7 @@ func init() {
ID_SCHEMA, "(optional) URL scheme",
ID_PORT, "(optional) server port",
ID_NAMESPACE, "vault namespace",
ID_SECRETENGINE, "secret engine",
ID_PATHPREFIX, "path prefix for secret",
})
cpi.RegisterStandardIdentity(CONSUMER_TYPE, identityMatcher,
Expand All @@ -75,7 +79,7 @@ The only supported auth methods, so far, are <code>token</code> and <code>approl
`)
}

func GetConsumerId(serverurl string, namespace string, secretpath ...string) (cpi.ConsumerIdentity, error) {
func GetConsumerId(serverurl string, namespace string, secretengine string, secretpath string) (cpi.ConsumerIdentity, error) {
if serverurl == "" {
return nil, errors.Newf("server address must be given")
}
Expand Down Expand Up @@ -105,16 +109,18 @@ func GetConsumerId(serverurl string, namespace string, secretpath ...string) (cp
if namespace != "" {
id[ID_NAMESPACE] = namespace
}
if secretengine != "" {
id[ID_SECRETENGINE] = secretengine
}

p := path.Join(secretpath...)
if p != "" {
id[ID_PATHPREFIX] = p
if secretpath != "" {
id[ID_PATHPREFIX] = secretpath
}
return id, nil
}

func GetCredentials(ctx cpi.ContextProvider, serverurl, namespace string, secretpath ...string) (cpi.Credentials, error) {
id, err := GetConsumerId(serverurl, namespace, secretpath...)
func GetCredentials(ctx cpi.ContextProvider, serverurl, namespace string, secretengine, secretpath string) (cpi.Credentials, error) {
id, err := GetConsumerId(serverurl, namespace, secretengine, secretpath)
if err != nil {
return nil, err
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/contexts/credentials/repositories/vault/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,14 @@ func (p *ConsumerProvider) update() error {
if err := client.SetToken(token); err != nil {
return err
}
if err := client.SetNamespace(p.repository.spec.Namespace); err != nil {
return err
}

// TODO: support for pure path based access for other secret engine types
secrets := slices.Clone(p.repository.spec.Secrets)
if len(secrets) == 0 {
s, err := client.Secrets.KvV2List(ctx, p.repository.spec.Path,
vault.WithNamespace(p.repository.spec.Namespace),
vault.WithMountPath(p.repository.spec.SecretsEngine))
if err != nil {
return err
Expand Down Expand Up @@ -175,7 +178,6 @@ func (p *ConsumerProvider) read(ctx context.Context, client *vault.Client, secre

secret = path.Join(p.repository.spec.Path, secret)
s, err := client.Secrets.KvV2Read(ctx, secret,
vault.WithNamespace(p.repository.spec.Namespace),
vault.WithMountPath(p.repository.spec.SecretsEngine))
if err != nil {
return nil, nil, nil, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/contexts/credentials/repositories/vault/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var (
)

func NewRepository(ctx cpi.Context, spec *RepositorySpec) (*Repository, error) {
id, err := identity.GetConsumerId(spec.ServerURL, spec.Namespace, spec.Path)
id, err := identity.GetConsumerId(spec.ServerURL, spec.Namespace, spec.SecretsEngine, spec.Path)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit a94128e

Please sign in to comment.