From c4285b29cccbae645033aba93ec76655d43e5cab Mon Sep 17 00:00:00 2001 From: Louis Ruch Date: Fri, 10 Jun 2022 12:26:19 -0700 Subject: [PATCH] fix(target): Correctly return egress credentials assocaited with target (#2167) --- internal/cmd/commands/targetscmd/funcs.go | 76 ++++++++++++------- .../handlers/targets/target_service.go | 75 ++++++++++-------- 2 files changed, 92 insertions(+), 59 deletions(-) diff --git a/internal/cmd/commands/targetscmd/funcs.go b/internal/cmd/commands/targetscmd/funcs.go index cf1d267bf..385c2c174 100644 --- a/internal/cmd/commands/targetscmd/funcs.go +++ b/internal/cmd/commands/targetscmd/funcs.go @@ -702,23 +702,6 @@ func printItemTable(result api.GenericResult) string { } var credentialSourceMaps map[credential.Purpose][]map[string]interface{} - if len(item.ApplicationCredentialSources) > 0 { - if credentialSourceMaps == nil { - credentialSourceMaps = make(map[credential.Purpose][]map[string]interface{}) - } - var applicationCredentialSourceMaps []map[string]interface{} - for _, lib := range item.ApplicationCredentialSources { - m := map[string]interface{}{ - "ID": lib.Id, - "Credential Store ID": lib.CredentialStoreId, - } - applicationCredentialSourceMaps = append(applicationCredentialSourceMaps, m) - } - credentialSourceMaps[credential.ApplicationPurpose] = applicationCredentialSourceMaps - if l := len("Credential Store ID"); l > maxLength { - maxLength = l - } - } if len(item.ApplicationCredentialLibraries) > 0 { if credentialSourceMaps == nil { credentialSourceMaps = make(map[credential.Purpose][]map[string]interface{}) @@ -736,6 +719,40 @@ func printItemTable(result api.GenericResult) string { maxLength = l } } + if len(item.ApplicationCredentialSources) > 0 { + if credentialSourceMaps == nil { + credentialSourceMaps = make(map[credential.Purpose][]map[string]interface{}) + } + var applicationCredentialSourceMaps []map[string]interface{} + for _, source := range item.ApplicationCredentialSources { + m := map[string]interface{}{ + "ID": source.Id, + "Credential Store ID": source.CredentialStoreId, + } + applicationCredentialSourceMaps = append(applicationCredentialSourceMaps, m) + } + credentialSourceMaps[credential.ApplicationPurpose] = applicationCredentialSourceMaps + if l := len("Credential Store ID"); l > maxLength { + maxLength = l + } + } + if len(item.EgressCredentialSources) > 0 { + if credentialSourceMaps == nil { + credentialSourceMaps = make(map[credential.Purpose][]map[string]interface{}) + } + var egressCredentialSourceMaps []map[string]interface{} + for _, source := range item.EgressCredentialSources { + m := map[string]interface{}{ + "ID": source.Id, + "Credential Store ID": source.CredentialStoreId, + } + egressCredentialSourceMaps = append(egressCredentialSourceMaps, m) + } + credentialSourceMaps[credential.EgressPurpose] = egressCredentialSourceMaps + if l := len("Credential Store ID"); l > maxLength { + maxLength = l + } + } ret := []string{ "", @@ -775,17 +792,22 @@ func printItemTable(result api.GenericResult) string { } } - if len(credentialSourceMaps) > 0 { - if appMap := credentialSourceMaps[credential.ApplicationPurpose]; len(appMap) > 0 { + for purpose, sources := range credentialSourceMaps { + switch purpose { + case credential.ApplicationPurpose: ret = append(ret, " Application Credential Sources:", ) - for _, m := range appMap { - ret = append(ret, - base.WrapMap(4, maxLength, m), - "", - ) - } + case credential.EgressPurpose: + ret = append(ret, + " Egress Credential Sources:", + ) + } + for _, m := range sources { + ret = append(ret, + base.WrapMap(4, maxLength, m), + "", + ) } } @@ -951,8 +973,8 @@ func exampleOutput() string { HostCatalogId: "hcst_1234567890", }, }, - ApplicationCredentialLibraryIds: []string{"clvlt_1234567890", "clvlt_0987654321"}, - ApplicationCredentialLibraries: []*targets.CredentialLibrary{ + ApplicationCredentialSourceIds: []string{"clvlt_1234567890", "clvlt_0987654321"}, + ApplicationCredentialSources: []*targets.CredentialSource{ { Id: "clvlt_1234567890", CredentialStoreId: "csvlt_1234567890", diff --git a/internal/daemon/controller/handlers/targets/target_service.go b/internal/daemon/controller/handlers/targets/target_service.go index 6e57826e3..9c312cbc3 100644 --- a/internal/daemon/controller/handlers/targets/target_service.go +++ b/internal/daemon/controller/handlers/targets/target_service.go @@ -1631,45 +1631,56 @@ func toProto(ctx context.Context, in target.Target, hostSources []target.HostSou }) } } - if outputFields.Has(globals.ApplicationCredentialLibraryIdsField) { - for _, cs := range credSources { - out.ApplicationCredentialLibraryIds = append(out.ApplicationCredentialLibraryIds, cs.Id()) + + var appCredSources, egressCredSources []*pb.CredentialSource + var appCredSourceIds, egressCredSourceIds []string + var appCredLibraries []*pb.CredentialLibrary + + for _, cs := range credSources { + switch cs.CredentialPurpose() { + case credential.ApplicationPurpose: + appCredSourceIds = append(appCredSourceIds, cs.Id()) + appCredSources = append(appCredSources, &pb.CredentialSource{ + Id: cs.Id(), + CredentialStoreId: cs.CredentialStoreId(), + }) + + // ApplicationCredentialLibrariesField is deprecated and should only be populated + // for application purpose + appCredLibraries = append(appCredLibraries, &pb.CredentialLibrary{ + Id: cs.Id(), + CredentialStoreId: cs.CredentialStoreId(), + }) + + case credential.EgressPurpose: + egressCredSources = append(egressCredSources, &pb.CredentialSource{ + Id: cs.Id(), + CredentialStoreId: cs.CredentialStoreId(), + }) + egressCredSourceIds = append(egressCredSourceIds, cs.Id()) + + default: + return nil, errors.New(ctx, errors.Internal, op, fmt.Sprintf("unrecognized purpose %q for credential source on target", cs.CredentialPurpose())) } } + + if outputFields.Has(globals.ApplicationCredentialLibraryIdsField) { + out.ApplicationCredentialLibraryIds = appCredSourceIds + } if outputFields.Has(globals.ApplicationCredentialSourceIdsField) { - for _, cs := range credSources { - out.ApplicationCredentialSourceIds = append(out.ApplicationCredentialSourceIds, cs.Id()) - } + out.ApplicationCredentialSourceIds = appCredSourceIds } if outputFields.Has(globals.ApplicationCredentialLibrariesField) { - for _, cs := range credSources { - switch credential.Purpose(cs.CredentialPurpose()) { - case credential.ApplicationPurpose: - out.ApplicationCredentialLibraries = append(out.ApplicationCredentialLibraries, &pb.CredentialLibrary{ - Id: cs.Id(), - CredentialStoreId: cs.CredentialStoreId(), - }) - case credential.IngressPurpose, credential.EgressPurpose: - // TODO: When we support other purposes add them to different fields here. - default: - return nil, errors.New(ctx, errors.Internal, op, fmt.Sprintf("unrecognized purpose %q for credential source on target", cs.CredentialPurpose())) - } - } + out.ApplicationCredentialLibraries = appCredLibraries } if outputFields.Has(globals.ApplicationCredentialSourcesField) { - for _, cs := range credSources { - switch credential.Purpose(cs.CredentialPurpose()) { - case credential.ApplicationPurpose: - out.ApplicationCredentialSources = append(out.ApplicationCredentialSources, &pb.CredentialSource{ - Id: cs.Id(), - CredentialStoreId: cs.CredentialStoreId(), - }) - case credential.IngressPurpose, credential.EgressPurpose: - // TODO: When we support other purposes add them to different fields here. - default: - return nil, errors.New(ctx, errors.Internal, op, fmt.Sprintf("unrecognized purpose %q for credential source on target", cs.CredentialPurpose())) - } - } + out.ApplicationCredentialSources = appCredSources + } + if outputFields.Has(globals.EgressCredentialSourceIdsField) { + out.EgressCredentialSourceIds = egressCredSourceIds + } + if outputFields.Has(globals.EgressCredentialSourcesField) { + out.EgressCredentialSources = egressCredSources } if outputFields.Has(globals.AttributesField) { if err := subtypeRegistry.setAttributes(in.GetType(), in, &out); err != nil {