Skip to content

Commit 78a4a24

Browse files
authored
Complete support for Oauth Vault permissions (#327)
1 parent 87b6b70 commit 78a4a24

4 files changed

Lines changed: 36 additions & 1 deletion

File tree

cmd/secrets/common/browser_flow.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,17 @@ const exchangeAuthCodeToTokenMutation = `mutation ExchangeAuthCodeToToken($reque
3737
}`
3838

3939
// vaultPermissionForMethod returns the API permission name for the given vault operation.
40+
// Names match the VaultPermission enum in platform GraphQL (createVaultAuthorizationUrl).
4041
func vaultPermissionForMethod(method string) (string, error) {
4142
switch method {
4243
case vaulttypes.MethodSecretsCreate:
4344
return "VAULT_PERMISSION_CREATE_SECRETS", nil
4445
case vaulttypes.MethodSecretsUpdate:
4546
return "VAULT_PERMISSION_UPDATE_SECRETS", nil
47+
case vaulttypes.MethodSecretsDelete:
48+
return "VAULT_PERMISSION_DELETE_SECRETS", nil
49+
case vaulttypes.MethodSecretsList:
50+
return "VAULT_PERMISSION_LIST_SECRETS", nil
4651
default:
4752
return "", fmt.Errorf("unsupported method: %s", method)
4853
}
@@ -110,6 +115,16 @@ func (h *Handler) executeBrowserUpsert(ctx context.Context, inputs UpsertSecrets
110115
return fmt.Errorf("unsupported method %q (expected %q or %q)", method, vaulttypes.MethodSecretsCreate, vaulttypes.MethodSecretsUpdate)
111116
}
112117

118+
return h.ExecuteBrowserVaultAuthorization(ctx, method, digest)
119+
}
120+
121+
// ExecuteBrowserVaultAuthorization completes platform OAuth for a vault JSON-RPC digest (create/update/delete/list).
122+
// It does not POST to the gateway; the short-lived vault JWT is for future DON submission.
123+
func (h *Handler) ExecuteBrowserVaultAuthorization(ctx context.Context, method string, digest [32]byte) error {
124+
if h.Credentials.AuthType == credentials.AuthTypeApiKey {
125+
return fmt.Errorf("this sign-in flow requires an interactive login; API keys are not supported")
126+
}
127+
113128
perm, err := vaultPermissionForMethod(method)
114129
if err != nil {
115130
return err

cmd/secrets/common/browser_flow_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,15 @@ func TestVaultPermissionForMethod(t *testing.T) {
2222
require.NoError(t, err)
2323
assert.Equal(t, "VAULT_PERMISSION_UPDATE_SECRETS", p)
2424

25-
_, err = vaultPermissionForMethod(vaulttypes.MethodSecretsDelete)
25+
p, err = vaultPermissionForMethod(vaulttypes.MethodSecretsDelete)
26+
require.NoError(t, err)
27+
assert.Equal(t, "VAULT_PERMISSION_DELETE_SECRETS", p)
28+
29+
p, err = vaultPermissionForMethod(vaulttypes.MethodSecretsList)
30+
require.NoError(t, err)
31+
assert.Equal(t, "VAULT_PERMISSION_LIST_SECRETS", p)
32+
33+
_, err = vaultPermissionForMethod("vault/secrets/unknown")
2634
require.Error(t, err)
2735
}
2836

cmd/secrets/delete/delete.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package delete
22

33
import (
4+
"context"
45
"encoding/hex"
56
"encoding/json"
67
"fmt"
@@ -164,6 +165,11 @@ func Execute(h *common.Handler, inputs DeleteSecretsInputs, duration time.Durati
164165
return fmt.Errorf("failed to calculate request digest: %w", err)
165166
}
166167

168+
if common.IsBrowserFlow(secretsAuth) {
169+
ui.Dim("Using your account to authorize vault access for this delete request...")
170+
return h.ExecuteBrowserVaultAuthorization(context.Background(), vaulttypes.MethodSecretsDelete, digest)
171+
}
172+
167173
gatewayPost := func() error {
168174
respBody, status, err := h.Gw.Post(requestBody)
169175
if err != nil {

cmd/secrets/list/list.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package list
22

33
import (
4+
"context"
45
"encoding/hex"
56
"encoding/json"
67
"fmt"
@@ -128,6 +129,11 @@ func Execute(h *common.Handler, namespace string, duration time.Duration, secret
128129
return fmt.Errorf("failed to marshal JSON-RPC request: %w", err)
129130
}
130131

132+
if common.IsBrowserFlow(secretsAuth) {
133+
ui.Dim("Using your account to authorize vault access for this list request...")
134+
return h.ExecuteBrowserVaultAuthorization(context.Background(), vaulttypes.MethodSecretsList, digest)
135+
}
136+
131137
ownerAddr := ethcommon.HexToAddress(owner)
132138

133139
allowlisted, err := h.Wrc.IsRequestAllowlisted(ownerAddr, digest)

0 commit comments

Comments
 (0)