feat(handlers): Allow UPD brokered creds to be associated w/ Targets + Session auth

This commit is contained in:
Hugo authored and irenarindos committed 2025-09-02 15:12:25 -04:00
1 parent f265ec4e5b
commit fbcc256742
5 files changed
+308 -58

No files matched your search

@@ -211,7 +211,25 @@ func staticToSessionCredential(ctx context.Context, cred credential.Static) (*pb
if err != nil {
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("creating proto struct for username password credential"))
}
case *credstatic.UsernamePasswordDomainCredential:
var err error
credType = string(globals.UsernamePasswordDomainCredentialType)
credData, err = handlers.ProtoToStruct(
ctx,
&pb.UsernamePasswordDomainCredential{
Username: c.GetUsername(),
Password: string(c.GetPassword()),
Domain: c.GetDomain(),
},
)
secret = map[string]any{
"username": c.GetUsername(),
"password": string(c.GetPassword()),
"domain": c.GetDomain(),
}
if err != nil {
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("creating proto struct for username password domain credential"))
}
case *credstatic.SshPrivateKeyCredential:
var err error
credType = string(globals.SshPrivateKeyCredentialType)
@@ -2048,6 +2048,7 @@ func validateAddCredentialSourcesRequest(req *pbs.AddTargetCredentialSourcesRequ
globals.VaultCredentialLibraryPrefix,
globals.UsernamePasswordCredentialPrefix,
globals.UsernamePasswordCredentialPreviousPrefix,
globals.UsernamePasswordDomainCredentialPrefix,
globals.SshPrivateKeyCredentialPrefix,
globals.JsonCredentialPrefix) {
badFields[globals.BrokeredCredentialSourceIdsField] = fmt.Sprintf("Incorrectly formatted credential source identifier %q.", cl)
@@ -2084,6 +2085,7 @@ func validateSetCredentialSourcesRequest(req *pbs.SetTargetCredentialSourcesRequ
globals.VaultCredentialLibraryPrefix,
globals.UsernamePasswordCredentialPrefix,
globals.UsernamePasswordCredentialPreviousPrefix,
globals.UsernamePasswordDomainCredentialPrefix,
globals.SshPrivateKeyCredentialPrefix,
globals.JsonCredentialPrefix) {
badFields[globals.BrokeredCredentialSourceIdsField] = fmt.Sprintf("Incorrectly formatted credential source identifier %q.", cl)
@@ -2124,6 +2126,7 @@ func validateRemoveCredentialSourcesRequest(req *pbs.RemoveTargetCredentialSourc
globals.VaultCredentialLibraryPrefix,
globals.UsernamePasswordCredentialPrefix,
globals.UsernamePasswordCredentialPreviousPrefix,
globals.UsernamePasswordDomainCredentialPrefix,
globals.SshPrivateKeyCredentialPrefix,
globals.JsonCredentialPrefix) {
badFields[globals.BrokeredCredentialSourceIdsField] = fmt.Sprintf("Incorrectly formatted credential source identifier %q.", cl)
@@ -3084,6 +3084,7 @@ func TestAddTargetCredentialSources(t *testing.T) {
storeStatic := credstatic.TestCredentialStore(t, conn, wrapper, proj.GetPublicId())
creds := credstatic.TestUsernamePasswordCredentials(t, conn, wrapper, "user", "pass", storeStatic.GetPublicId(), proj.GetPublicId(), 2)
updCreds := credstatic.TestUsernamePasswordDomainCredentials(t, conn, wrapper, "user", "pass", "domain", storeStatic.GetPublicId(), proj.GetPublicId(), 2)
addCases := []struct {
name string
@@ -3103,6 +3104,12 @@ func TestAddTargetCredentialSources(t *testing.T) {
addSources: []string{creds[1].GetPublicId()},
resultSourceIds: []string{creds[1].GetPublicId()},
},
{
name: "Add static upd cred on empty target",
tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "empty for static_upd sources"),
addSources: []string{updCreds[1].GetPublicId()},
resultSourceIds: []string{updCreds[1].GetPublicId()},
},
{
name: "Add library on library populated target",
tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "populated for lib-lib sources", target.WithCredentialLibraries([]*target.CredentialLibrary{target.TestNewCredentialLibrary("", cls[0].GetPublicId(), credential.BrokeredPurpose)})),
@@ -3115,18 +3122,36 @@ func TestAddTargetCredentialSources(t *testing.T) {
addSources: []string{cls[1].GetPublicId()},
resultSourceIds: []string{creds[0].GetPublicId(), cls[1].GetPublicId()},
},
{
name: "Add library on static upd cred populated target",
tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "populated for lib-static_upd sources", target.WithStaticCredentials([]*target.StaticCredential{target.TestNewStaticCredential("", updCreds[0].GetPublicId(), credential.BrokeredPurpose)})),
addSources: []string{cls[1].GetPublicId()},
resultSourceIds: []string{updCreds[0].GetPublicId(), cls[1].GetPublicId()},
},
{
name: "Add static cred on library populated target",
tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "populated for static-lib sources", target.WithCredentialLibraries([]*target.CredentialLibrary{target.TestNewCredentialLibrary("", cls[0].GetPublicId(), credential.BrokeredPurpose)})),
addSources: []string{creds[1].GetPublicId()},
resultSourceIds: []string{cls[0].GetPublicId(), creds[1].GetPublicId()},
},
{
name: "Add upd static cred on library populated target",
tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "populated for static_upd-lib sources", target.WithCredentialLibraries([]*target.CredentialLibrary{target.TestNewCredentialLibrary("", cls[0].GetPublicId(), credential.BrokeredPurpose)})),
addSources: []string{updCreds[1].GetPublicId()},
resultSourceIds: []string{cls[0].GetPublicId(), updCreds[1].GetPublicId()},
},
{
name: "Add static cred on static cred populated target",
tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "populated for static-static sources", target.WithStaticCredentials([]*target.StaticCredential{target.TestNewStaticCredential("", creds[0].GetPublicId(), credential.BrokeredPurpose)})),
addSources: []string{creds[1].GetPublicId()},
resultSourceIds: []string{creds[0].GetPublicId(), creds[1].GetPublicId()},
},
{
name: "Add static upd cred on static cred populated target",
tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "populated for static_upd-static sources", target.WithStaticCredentials([]*target.StaticCredential{target.TestNewStaticCredential("", creds[0].GetPublicId(), credential.BrokeredPurpose)})),
addSources: []string{updCreds[1].GetPublicId()},
resultSourceIds: []string{creds[0].GetPublicId(), updCreds[1].GetPublicId()},
},
{
name: "Add duplicated sources on library populated target",
tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "duplicated for lib sources", target.WithCredentialLibraries([]*target.CredentialLibrary{target.TestNewCredentialLibrary("", cls[0].GetPublicId(), credential.BrokeredPurpose)})),
@@ -3139,6 +3164,12 @@ func TestAddTargetCredentialSources(t *testing.T) {
addSources: []string{cls[1].GetPublicId(), cls[1].GetPublicId(), creds[1].GetPublicId(), creds[1].GetPublicId()},
resultSourceIds: []string{creds[0].GetPublicId(), cls[1].GetPublicId(), creds[1].GetPublicId()},
},
{
name: "Add duplicated sources on static upd cred populated target",
tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "duplicated for static upd sources", target.WithStaticCredentials([]*target.StaticCredential{target.TestNewStaticCredential("", updCreds[0].GetPublicId(), credential.BrokeredPurpose)})),
addSources: []string{cls[1].GetPublicId(), cls[1].GetPublicId(), updCreds[1].GetPublicId(), updCreds[1].GetPublicId()},
resultSourceIds: []string{updCreds[0].GetPublicId(), cls[1].GetPublicId(), updCreds[1].GetPublicId()},
},
}
for _, tc := range addCases {
@@ -3264,6 +3295,7 @@ func TestSetTargetCredentialSources(t *testing.T) {
storeStatic := credstatic.TestCredentialStore(t, conn, wrapper, proj.GetPublicId())
creds := credstatic.TestUsernamePasswordCredentials(t, conn, wrapper, "user", "pass", storeStatic.GetPublicId(), proj.GetPublicId(), 2)
updCreds := credstatic.TestUsernamePasswordDomainCredentials(t, conn, wrapper, "user", "pass", "domain", storeStatic.GetPublicId(), proj.GetPublicId(), 2)
setCases := []struct {
name string
@@ -3283,6 +3315,12 @@ func TestSetTargetCredentialSources(t *testing.T) {
setCredentialSources: []string{creds[1].GetPublicId()},
resultCredentialSourceIds: []string{creds[1].GetPublicId()},
},
{
name: "Set static_upd on empty target",
tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "empty static_upd"),
setCredentialSources: []string{updCreds[1].GetPublicId()},
resultCredentialSourceIds: []string{updCreds[1].GetPublicId()},
},
{
name: "Set library on library populated target",
tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "populated library-library", target.WithCredentialLibraries([]*target.CredentialLibrary{target.TestNewCredentialLibrary("", cls[0].GetPublicId(), credential.BrokeredPurpose)})),
@@ -3295,18 +3333,42 @@ func TestSetTargetCredentialSources(t *testing.T) {
setCredentialSources: []string{creds[1].GetPublicId()},
resultCredentialSourceIds: []string{creds[1].GetPublicId()},
},
{
name: "Set static_upd on library populated target",
tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "populated static_upd-library", target.WithCredentialLibraries([]*target.CredentialLibrary{target.TestNewCredentialLibrary("", cls[0].GetPublicId(), credential.BrokeredPurpose)})),
setCredentialSources: []string{updCreds[1].GetPublicId()},
resultCredentialSourceIds: []string{updCreds[1].GetPublicId()},
},
{
name: "Set library on static populated target",
tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "populated library-static", target.WithStaticCredentials([]*target.StaticCredential{target.TestNewStaticCredential("", creds[0].GetPublicId(), credential.BrokeredPurpose)})),
setCredentialSources: []string{cls[1].GetPublicId()},
resultCredentialSourceIds: []string{cls[1].GetPublicId()},
},
{
name: "Set library on static_upd populated target",
tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "populated library-static_upd", target.WithStaticCredentials([]*target.StaticCredential{target.TestNewStaticCredential("", updCreds[0].GetPublicId(), credential.BrokeredPurpose)})),
setCredentialSources: []string{cls[1].GetPublicId()},
resultCredentialSourceIds: []string{cls[1].GetPublicId()},
},
{
name: "Set static on static populated target",
tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "populated static-static", target.WithStaticCredentials([]*target.StaticCredential{target.TestNewStaticCredential("", creds[0].GetPublicId(), credential.BrokeredPurpose)})),
setCredentialSources: []string{creds[1].GetPublicId()},
resultCredentialSourceIds: []string{creds[1].GetPublicId()},
},
{
name: "Set static_upd on static populated target",
tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "populated static_upd-static", target.WithStaticCredentials([]*target.StaticCredential{target.TestNewStaticCredential("", creds[0].GetPublicId(), credential.BrokeredPurpose)})),
setCredentialSources: []string{updCreds[1].GetPublicId()},
resultCredentialSourceIds: []string{updCreds[1].GetPublicId()},
},
{
name: "Set static on static_upd populated target",
tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "populated static-static_upd", target.WithStaticCredentials([]*target.StaticCredential{target.TestNewStaticCredential("", updCreds[0].GetPublicId(), credential.BrokeredPurpose)})),
setCredentialSources: []string{creds[1].GetPublicId()},
resultCredentialSourceIds: []string{creds[1].GetPublicId()},
},
{
name: "Set duplicate library on populated target",
tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "duplicate library", target.WithCredentialLibraries([]*target.CredentialLibrary{target.TestNewCredentialLibrary("", cls[0].GetPublicId(), credential.BrokeredPurpose)})),
@@ -3319,11 +3381,19 @@ func TestSetTargetCredentialSources(t *testing.T) {
setCredentialSources: []string{creds[1].GetPublicId(), creds[1].GetPublicId()},
resultCredentialSourceIds: []string{creds[1].GetPublicId()},
},
{
name: "Set duplicate static_upd on populated target",
tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "duplicate static_upd", target.WithStaticCredentials([]*target.StaticCredential{target.TestNewStaticCredential("", updCreds[0].GetPublicId(), credential.BrokeredPurpose)})),
setCredentialSources: []string{updCreds[1].GetPublicId(), updCreds[1].GetPublicId()},
resultCredentialSourceIds: []string{updCreds[1].GetPublicId()},
},
{
name: "Set empty on populated target",
tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "another populated",
target.WithCredentialLibraries([]*target.CredentialLibrary{target.TestNewCredentialLibrary("", cls[0].GetPublicId(), credential.BrokeredPurpose)}),
target.WithStaticCredentials([]*target.StaticCredential{target.TestNewStaticCredential("", creds[0].GetPublicId(), credential.BrokeredPurpose)})),
target.WithStaticCredentials([]*target.StaticCredential{target.TestNewStaticCredential("", creds[0].GetPublicId(), credential.BrokeredPurpose)}),
target.WithStaticCredentials([]*target.StaticCredential{target.TestNewStaticCredential("", updCreds[0].GetPublicId(), credential.BrokeredPurpose)}),
),
setCredentialSources: []string{},
resultCredentialSourceIds: nil,
},
@@ -3438,6 +3508,7 @@ func TestRemoveTargetCredentialSources(t *testing.T) {
csStatic := credstatic.TestCredentialStores(t, conn, wrapper, proj.GetPublicId(), 1)[0]
creds := credstatic.TestUsernamePasswordCredentials(t, conn, wrapper, "u", "p", csStatic.GetPublicId(), proj.GetPublicId(), 2)
updCreds := credstatic.TestUsernamePasswordDomainCredentials(t, conn, wrapper, "user", "pass", "domain", csStatic.GetPublicId(), proj.GetPublicId(), 2)
removeCases := []struct {
name string
@@ -3458,6 +3529,12 @@ func TestRemoveTargetCredentialSources(t *testing.T) {
removeCredentialSources: []string{creds[1].GetPublicId()},
wantErr: true,
},
{
name: "Remove static_upd from empty",
tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "empty static_upd"),
removeCredentialSources: []string{updCreds[1].GetPublicId()},
wantErr: true,
},
{
name: "Remove 1 of 2 libraries",
tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "remove partial lib",
@@ -3478,6 +3555,16 @@ func TestRemoveTargetCredentialSources(t *testing.T) {
removeCredentialSources: []string{creds[1].GetPublicId()},
resultCredentialSourceIds: []string{creds[0].GetPublicId()},
},
{
name: "Remove 1 of 2 static_upd credentials",
tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "remove partial static_upd",
target.WithStaticCredentials([]*target.StaticCredential{
target.TestNewStaticCredential("", updCreds[0].GetPublicId(), credential.BrokeredPurpose),
target.TestNewStaticCredential("", updCreds[1].GetPublicId(), credential.BrokeredPurpose),
})),
removeCredentialSources: []string{updCreds[1].GetPublicId()},
resultCredentialSourceIds: []string{updCreds[0].GetPublicId()},
},
{
name: "Remove 1 duplicate set of 2 libraries",
tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "remove duplicate lib",
@@ -3502,6 +3589,18 @@ func TestRemoveTargetCredentialSources(t *testing.T) {
},
resultCredentialSourceIds: []string{creds[0].GetPublicId()},
},
{
name: "Remove 1 duplicate set of 2 static_upd credentials",
tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "remove duplicate static_upd",
target.WithStaticCredentials([]*target.StaticCredential{
target.TestNewStaticCredential("", updCreds[0].GetPublicId(), credential.BrokeredPurpose),
target.TestNewStaticCredential("", updCreds[1].GetPublicId(), credential.BrokeredPurpose),
})),
removeCredentialSources: []string{
updCreds[1].GetPublicId(), updCreds[1].GetPublicId(),
},
resultCredentialSourceIds: []string{updCreds[0].GetPublicId()},
},
{
name: "Remove mixed sources from target",
tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "remove mixed",
@@ -3512,12 +3611,14 @@ func TestRemoveTargetCredentialSources(t *testing.T) {
target.WithStaticCredentials([]*target.StaticCredential{
target.TestNewStaticCredential("", creds[0].GetPublicId(), credential.BrokeredPurpose),
target.TestNewStaticCredential("", creds[1].GetPublicId(), credential.BrokeredPurpose),
target.TestNewStaticCredential("", updCreds[0].GetPublicId(), credential.BrokeredPurpose),
target.TestNewStaticCredential("", updCreds[1].GetPublicId(), credential.BrokeredPurpose),
})),
removeCredentialSources: []string{
cls[1].GetPublicId(), creds[0].GetPublicId(),
cls[1].GetPublicId(), creds[0].GetPublicId(), updCreds[0].GetPublicId(),
},
resultCredentialSourceIds: []string{
cls[0].GetPublicId(), creds[1].GetPublicId(),
cls[0].GetPublicId(), creds[1].GetPublicId(), updCreds[1].GetPublicId(),
},
},
{
@@ -3530,10 +3631,13 @@ func TestRemoveTargetCredentialSources(t *testing.T) {
target.WithStaticCredentials([]*target.StaticCredential{
target.TestNewStaticCredential("", creds[0].GetPublicId(), credential.BrokeredPurpose),
target.TestNewStaticCredential("", creds[1].GetPublicId(), credential.BrokeredPurpose),
target.TestNewStaticCredential("", updCreds[0].GetPublicId(), credential.BrokeredPurpose),
target.TestNewStaticCredential("", updCreds[1].GetPublicId(), credential.BrokeredPurpose),
})),
removeCredentialSources: []string{
cls[0].GetPublicId(), cls[1].GetPublicId(),
creds[0].GetPublicId(), creds[1].GetPublicId(),
updCreds[0].GetPublicId(), updCreds[1].GetPublicId(),
},
resultCredentialSourceIds: []string{},
},
@@ -4125,6 +4229,22 @@ func TestAuthorizeSessionTypedCredentials(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, upCredResp)
updCredResp, err := credService.CreateCredential(ctx, &pbs.CreateCredentialRequest{Item: &credpb.Credential{
CredentialStoreId: staticStore.GetPublicId(),
Type: credential.UsernamePasswordDomainSubtype.String(),
Name: wrapperspb.String("UPD Cred Name"),
Description: wrapperspb.String("UPD Cred Description"),
Attrs: &credpb.Credential_UsernamePasswordDomainAttributes{
UsernamePasswordDomainAttributes: &credpb.UsernamePasswordDomainAttributes{
Username: wrapperspb.String("static-username"),
Password: wrapperspb.String("static-password"),
Domain: wrapperspb.String("static-domain"),
},
},
}})
require.NoError(t, err)
require.NotNil(t, updCredResp)
sshPkCredResp, err := credService.CreateCredential(ctx, &pbs.CreateCredentialRequest{Item: &credpb.Credential{
CredentialStoreId: staticStore.GetPublicId(),
Type: credential.SshPrivateKeySubtype.String(),
@@ -4350,6 +4470,34 @@ func TestAuthorizeSessionTypedCredentials(t *testing.T) {
},
wantedConnectionLimit: 1000,
},
{
name: "static-UsernamePasswordDomain",
hostSourceId: shs.GetPublicId(),
credSourceId: updCredResp.GetItem().GetId(),
wantedHostId: h.GetPublicId(),
wantedEndpoint: h.GetAddress(),
wantedCred: &pb.SessionCredential{
CredentialSource: &pb.CredentialSource{
Id: updCredResp.GetItem().GetId(),
Name: updCredResp.GetItem().GetName().GetValue(),
Description: updCredResp.GetItem().GetDescription().GetValue(),
CredentialStoreId: staticStore.GetPublicId(),
Type: credstatic.Subtype.String(),
CredentialType: string(globals.UsernamePasswordDomainCredentialType),
},
Credential: func() *structpb.Struct {
data := map[string]any{
"password": "static-password",
"username": "static-username",
"domain": "static-domain",
}
st, err := structpb.NewStruct(data)
require.NoError(t, err)
return st
}(),
},
wantedConnectionLimit: 1000,
},
{
name: "static-ssh-private-key",
hostSourceId: shs.GetPublicId(),