Skip to content

Commit e5df91a

Browse files
Merge remote-tracking branch 'remotes/from/ce/main'
2 parents 5016dd7 + 3fde566 commit e5df91a

File tree

7 files changed

+127
-38
lines changed

7 files changed

+127
-38
lines changed

builtin/credential/approle/path_login_test.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"time"
1111

1212
"github.com/hashicorp/vault/sdk/logical"
13+
"github.com/stretchr/testify/require"
1314
)
1415

1516
func TestAppRole_BoundCIDRLogin(t *testing.T) {
@@ -358,3 +359,90 @@ func TestAppRole_RoleDoesNotExist(t *testing.T) {
358359
t.Fatalf("Error was not due to invalid role ID. Error: %s", errString)
359360
}
360361
}
362+
363+
// TestAppRole_RoleLogin_AliasMetadata tests that the alias metadata is correctly set
364+
// in the role and that it is returned in the login response.
365+
func TestAppRole_RoleLogin_AliasMetadata(t *testing.T) {
366+
b, storage := createBackendWithStorage(t)
367+
368+
metadata := map[string]string{
369+
"key1": "value1",
370+
"key2": "value2",
371+
}
372+
373+
// Create a role with token auth metadata
374+
{
375+
roleData := map[string]interface{}{
376+
"policies": "a,b,c",
377+
"alias_metadata": metadata,
378+
}
379+
req := &logical.Request{
380+
Operation: logical.CreateOperation,
381+
Path: "role/role1",
382+
Storage: storage,
383+
Data: roleData,
384+
}
385+
_ = b.requestNoErr(t, req)
386+
}
387+
388+
// Assert that the role was created with the correct metadata
389+
{
390+
roleRoleIDReq := &logical.Request{
391+
Operation: logical.ReadOperation,
392+
Path: "role/role1",
393+
Storage: storage,
394+
}
395+
resp := b.requestNoErr(t, roleRoleIDReq)
396+
require.Equal(t, metadata, resp.Data["alias_metadata"])
397+
}
398+
399+
// Get the role ID
400+
var roleID any
401+
{
402+
roleRoleIDReq := &logical.Request{
403+
Operation: logical.ReadOperation,
404+
Path: "role/role1/role-id",
405+
Storage: storage,
406+
}
407+
resp := b.requestNoErr(t, roleRoleIDReq)
408+
409+
roleID = resp.Data["role_id"]
410+
}
411+
412+
// Get the secret ID
413+
var secretID any
414+
{
415+
roleSecretIDReq := &logical.Request{
416+
Operation: logical.UpdateOperation,
417+
Path: "role/role1/secret-id",
418+
Storage: storage,
419+
}
420+
resp := b.requestNoErr(t, roleSecretIDReq)
421+
422+
secretID = resp.Data["secret_id"]
423+
}
424+
425+
// Login
426+
{
427+
loginData := map[string]interface{}{
428+
"role_id": roleID,
429+
"secret_id": secretID,
430+
}
431+
loginReq := &logical.Request{
432+
Operation: logical.UpdateOperation,
433+
Path: "login",
434+
Storage: storage,
435+
Data: loginData,
436+
Connection: &logical.Connection{
437+
RemoteAddr: "127.0.0.1",
438+
},
439+
}
440+
loginResp, err := b.HandleRequest(context.Background(), loginReq)
441+
require.NoError(t, err)
442+
require.False(t, loginResp.IsError())
443+
444+
require.NotNil(t, loginResp.Auth, "expected a non-nil auth object in the response")
445+
446+
require.Equal(t, metadata, loginResp.Auth.Alias.CustomMetadata)
447+
}
448+
}

builtin/credential/aws/path_role_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"github.com/hashicorp/go-hclog"
1515
"github.com/hashicorp/go-secure-stdlib/awsutil"
1616
"github.com/hashicorp/go-secure-stdlib/strutil"
17-
"github.com/hashicorp/vault/helper/constants"
1817
vlttesting "github.com/hashicorp/vault/helper/testhelpers/logical"
1918
"github.com/hashicorp/vault/sdk/helper/logging"
2019
"github.com/hashicorp/vault/sdk/helper/policyutil"
@@ -603,6 +602,7 @@ func TestAwsEc2_RoleCrud(t *testing.T) {
603602
}
604603

605604
expected := map[string]interface{}{
605+
"alias_metadata": map[string]string{},
606606
"auth_type": ec2AuthType,
607607
"bound_ami_id": []string{"testamiid"},
608608
"bound_account_id": []string{"testaccountid"},
@@ -635,10 +635,6 @@ func TestAwsEc2_RoleCrud(t *testing.T) {
635635
"token_type": "default",
636636
}
637637

638-
if constants.IsEnterprise {
639-
expected["alias_metadata"] = map[string]string{}
640-
}
641-
642638
if resp.Data["role_id"] == nil {
643639
t.Fatal("role_id not found in response")
644640
}

builtin/credential/ldap/backend_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"github.com/go-test/deep"
1717
hclog "github.com/hashicorp/go-hclog"
1818
"github.com/hashicorp/go-secure-stdlib/strutil"
19-
"github.com/hashicorp/vault/helper/constants"
2019
"github.com/hashicorp/vault/helper/namespace"
2120
"github.com/hashicorp/vault/helper/testhelpers/ldap"
2221
logicaltest "github.com/hashicorp/vault/helper/testhelpers/logical"
@@ -1522,6 +1521,7 @@ func TestLdapAuthBackend_ConfigUpgrade(t *testing.T) {
15221521
TokenParams: tokenutil.TokenParams{
15231522
TokenPeriod: 5 * time.Minute,
15241523
TokenExplicitMaxTTL: 24 * time.Hour,
1524+
AliasMetadata: make(map[string]string),
15251525
},
15261526
ConfigEntry: &ldaputil.ConfigEntry{
15271527
Url: cfg.Url,
@@ -1545,9 +1545,6 @@ func TestLdapAuthBackend_ConfigUpgrade(t *testing.T) {
15451545
MaximumPageSize: 1000,
15461546
},
15471547
}
1548-
if constants.IsEnterprise {
1549-
exp.TokenParams.AliasMetadata = make(map[string]string)
1550-
}
15511548

15521549
configEntry, err := b.Config(ctx, configReq)
15531550
if err != nil {

changelog/_11468.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:improvement
2+
sdk: Add alias_metadata to tokenutil fields that auth method roles use.
3+
```

sdk/helper/tokenutil/tokenutil.go

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func AddTokenFieldsWithAllowList(m map[string]*framework.FieldSchema, allowed []
7676

7777
// TokenFields provides a set of field schemas for the parameters
7878
func TokenFields() map[string]*framework.FieldSchema {
79-
return entTokenFields(map[string]*framework.FieldSchema{
79+
return map[string]*framework.FieldSchema{
8080
"token_bound_cidrs": {
8181
Type: framework.TypeCommaStringSlice,
8282
Description: `Comma separated string or JSON list of CIDR blocks. If set, specifies the blocks of IP addresses which are allowed to use the generated token.`,
@@ -160,7 +160,16 @@ func TokenFields() map[string]*framework.FieldSchema {
160160
Group: "Tokens",
161161
},
162162
},
163-
})
163+
164+
"alias_metadata": {
165+
Type: framework.TypeKVPairs,
166+
Description: "The metadata to be tied to generated entity alias. This should be a list or map containing the metadata in key value pairs",
167+
DisplayAttrs: &framework.DisplayAttributes{
168+
Name: "Token Alias Metadata",
169+
Group: "Tokens",
170+
},
171+
},
172+
}
164173
}
165174

166175
// ParseTokenFields provides common field parsing functionality into a TokenFields struct
@@ -241,7 +250,10 @@ func (t *TokenParams) ParseTokenFields(req *logical.Request, d *framework.FieldD
241250
return errors.New("'token_ttl' cannot be greater than 'token_max_ttl'")
242251
}
243252

244-
t.entParseTokenFields(d)
253+
t.AliasMetadata = make(map[string]string)
254+
if tokenMetadataRaw, ok := d.GetOk("alias_metadata"); ok {
255+
t.AliasMetadata = tokenMetadataRaw.(map[string]string)
256+
}
245257

246258
return nil
247259
}
@@ -266,7 +278,10 @@ func (t *TokenParams) PopulateTokenData(m map[string]interface{}) {
266278
m["token_bound_cidrs"] = []string{}
267279
}
268280

269-
t.entPopulateTokenData(m)
281+
m["alias_metadata"] = map[string]string{}
282+
if len(t.AliasMetadata) > 0 {
283+
m["alias_metadata"] = t.AliasMetadata
284+
}
270285
}
271286

272287
// PopulateTokenAuth populates Auth with parameters
@@ -282,7 +297,17 @@ func (t *TokenParams) PopulateTokenAuth(auth *logical.Auth) {
282297
auth.TTL = t.TokenTTL
283298
auth.NumUses = t.TokenNumUses
284299

285-
t.entPopulateTokenAuth(auth)
300+
if len(t.AliasMetadata) > 0 && auth.Alias != nil {
301+
if auth.Alias.CustomMetadata == nil {
302+
auth.Alias.CustomMetadata = map[string]string{}
303+
}
304+
for k, v := range t.AliasMetadata {
305+
if _, ok := auth.Alias.CustomMetadata[k]; !ok {
306+
// Do not override metadata with the same key added by the caller
307+
auth.Alias.CustomMetadata[k] = v
308+
}
309+
}
310+
}
286311
}
287312

288313
func DeprecationText(param string) string {

sdk/helper/tokenutil/tokenutil_stubs_oss.go

Lines changed: 0 additions & 21 deletions
This file was deleted.

vault/seal_autoseal.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ func (d *autoSeal) StartHealthCheck(ctx context.Context) {
481481
go func() {
482482
lastTestOk := true
483483
lastSeenOk := time.Now()
484+
core := d.core
484485

485486
check := func(now time.Time) {
486487
ctx, cancel := context.WithTimeout(ctx, seal.HealthTestTimeout)
@@ -504,7 +505,7 @@ error and restart Vault.`)
504505
if err := sealWrapper.CheckHealth(ctx, now); err != nil {
505506
// Seal wrapper is unhealthy
506507
d.logger.Warn("seal wrapper health check failed", "seal_name", sealWrapper.Name, "err", err)
507-
d.core.MetricSink().SetGaugeWithLabels(autoSealUnavailableDuration,
508+
core.MetricSink().SetGaugeWithLabels(autoSealUnavailableDuration,
508509
float32(time.Since(sealWrapper.LastSeenHealthy()).Milliseconds()), mLabels)
509510
allHealthy = false
510511
} else {
@@ -525,14 +526,14 @@ error and restart Vault.`)
525526
lastTestOk = true
526527
lastSeenOk = now
527528
healthCheck.Reset(seal.HealthTestIntervalNominal)
528-
d.core.MetricSink().SetGauge(autoSealUnavailableDuration, 0)
529+
core.MetricSink().SetGauge(autoSealUnavailableDuration, 0)
529530
} else {
530531
if lastTestOk && allUnhealthy {
531532
d.logger.Error("seal backend is completely unhealthy (all seal wrappers all unhealthy)", "downtime", now.Sub(lastSeenOk).String())
532533
}
533534
lastTestOk = false
534535
healthCheck.Reset(seal.HealthTestIntervalUnhealthy)
535-
d.core.MetricSink().SetGauge(autoSealUnavailableDuration, float32(time.Since(lastSeenOk).Milliseconds()))
536+
core.MetricSink().SetGauge(autoSealUnavailableDuration, float32(time.Since(lastSeenOk).Milliseconds()))
536537
}
537538

538539
d.hcLock.Lock()

0 commit comments

Comments
 (0)