feat(target): Decode based on credential type
This commit is contained in:
1 parent
591ec476e7
commit
a90f1f4eb1
9 files changed
+736
-298
No files matched your search
@@ -47,21 +47,24 @@ func generateCredentialTableOutputSlice(prefixIndent int, creds []*targets.Sessi
|
||||
var ret []string
|
||||
prefixString := strings.Repeat(" ", prefixIndent)
|
||||
for _, crd := range creds {
|
||||
libMap := map[string]interface{}{
|
||||
"Credential Store ID": crd.CredentialLibrary.CredentialStoreId,
|
||||
"Credential Library ID": crd.CredentialLibrary.Id,
|
||||
"Credential Store Type": crd.CredentialLibrary.Type,
|
||||
credMap := map[string]interface{}{
|
||||
"Credential Store ID": crd.CredentialSource.CredentialStoreId,
|
||||
"Credential Source ID": crd.CredentialSource.Id,
|
||||
"Credential Store Type": crd.CredentialSource.Type,
|
||||
}
|
||||
if crd.CredentialLibrary.Name != "" {
|
||||
libMap["Credential Library Name"] = crd.CredentialLibrary.Name
|
||||
if crd.CredentialSource.Name != "" {
|
||||
credMap["Credential Source Name"] = crd.CredentialSource.Name
|
||||
}
|
||||
if crd.CredentialLibrary.Description != "" {
|
||||
libMap["Credential Library Description"] = crd.CredentialLibrary.Description
|
||||
if crd.CredentialSource.Description != "" {
|
||||
credMap["Credential Source Description"] = crd.CredentialSource.Description
|
||||
}
|
||||
maxLength := base.MaxAttributesLength(libMap, nil, nil)
|
||||
if crd.CredentialSource.CredentialType != "" {
|
||||
credMap["Credential Type"] = crd.CredentialSource.CredentialType
|
||||
}
|
||||
maxLength := base.MaxAttributesLength(credMap, nil, nil)
|
||||
ret = append(ret,
|
||||
fmt.Sprintf("%sCredentials:", prefixString),
|
||||
base.WrapMap(2+prefixIndent, maxLength, libMap),
|
||||
base.WrapMap(2+prefixIndent, maxLength, credMap),
|
||||
fmt.Sprintf("%s Secret:", prefixString))
|
||||
ret = append(ret,
|
||||
fmtSecretForTable(2+prefixIndent, crd)...,
|
||||
@@ -74,8 +77,18 @@ func generateCredentialTableOutputSlice(prefixIndent int, creds []*targets.Sessi
|
||||
func fmtSecretForTable(indent int, sc *targets.SessionCredential) []string {
|
||||
prefixStr := strings.Repeat(" ", indent)
|
||||
origSecret := []string{fmt.Sprintf("%s %s", prefixStr, sc.Secret.Raw)}
|
||||
switch sc.CredentialLibrary.Type {
|
||||
switch sc.CredentialSource.Type {
|
||||
case "vault":
|
||||
if sc.Credential != nil {
|
||||
maxLength := 0
|
||||
for k := range sc.Credential {
|
||||
if len(k) > maxLength {
|
||||
maxLength = len(k)
|
||||
}
|
||||
}
|
||||
return []string{fmt.Sprintf("%s %s", prefixStr, base.WrapMap(2, maxLength+2, sc.Credential))}
|
||||
}
|
||||
|
||||
in, err := base64.StdEncoding.DecodeString(strings.Trim(string(sc.Secret.Raw), `"`))
|
||||
if err != nil {
|
||||
return origSecret
|
||||
|
||||
@@ -847,7 +847,7 @@ func printCustomActionOutputImpl(c *Command) (bool, error) {
|
||||
}
|
||||
|
||||
ret = append(ret,
|
||||
fmt.Sprintf(" Credential Store ID: %s", cred.CredentialSource.CredentialStoreId),
|
||||
fmt.Sprintf(" Credential Store ID: %s", cred.CredentialSource.CredentialStoreId),
|
||||
fmt.Sprintf(" Credential Source ID: %s", cred.CredentialSource.Id),
|
||||
fmt.Sprintf(" Credential Source Type: %s", cred.CredentialSource.Type))
|
||||
|
||||
@@ -859,26 +859,43 @@ func printCustomActionOutputImpl(c *Command) (bool, error) {
|
||||
ret = append(ret,
|
||||
fmt.Sprintf(" Credential Source Description: %s", cred.CredentialSource.Description))
|
||||
}
|
||||
if cred.CredentialSource.CredentialType != "" {
|
||||
ret = append(ret,
|
||||
fmt.Sprintf(" Credential Type: %s", cred.CredentialSource.CredentialType))
|
||||
}
|
||||
|
||||
var secretStr []string
|
||||
switch cred.CredentialSource.Type {
|
||||
case "vault":
|
||||
// If it's Vault, the result will be JSON, except in
|
||||
// specific circumstances that aren't used for
|
||||
// credential fetching. So we can take the bytes
|
||||
// as-is (after base64-decoding), but we'll format
|
||||
// it nicely.
|
||||
in, err := base64.StdEncoding.DecodeString(strings.Trim(string(cred.Secret.Raw), `"`))
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("Error decoding secret as base64: %w", err)
|
||||
}
|
||||
dst := new(bytes.Buffer)
|
||||
if err := json.Indent(dst, in, " ", " "); err != nil {
|
||||
return false, fmt.Errorf("Error pretty-printing JSON: %w", err)
|
||||
}
|
||||
secretStr = strings.Split(dst.String(), "\n")
|
||||
if len(secretStr) > 0 {
|
||||
// Indent doesn't apply to the first line 🙄
|
||||
secretStr[0] = fmt.Sprintf(" %s", secretStr[0])
|
||||
switch {
|
||||
case cred.Credential != nil:
|
||||
maxLength := 0
|
||||
for k := range cred.Credential {
|
||||
if len(k) > maxLength {
|
||||
maxLength = len(k)
|
||||
}
|
||||
}
|
||||
secretStr = []string{fmt.Sprintf(" %s", base.WrapMap(2, maxLength+2, cred.Credential))}
|
||||
|
||||
default:
|
||||
// If it's Vault, the result will be JSON, except in
|
||||
// specific circumstances that aren't used for
|
||||
// credential fetching. So we can take the bytes
|
||||
// as-is (after base64-decoding), but we'll format
|
||||
// it nicely.
|
||||
in, err := base64.StdEncoding.DecodeString(strings.Trim(string(cred.Secret.Raw), `"`))
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("Error decoding secret as base64: %w", err)
|
||||
}
|
||||
dst := new(bytes.Buffer)
|
||||
if err := json.Indent(dst, in, " ", " "); err != nil {
|
||||
return false, fmt.Errorf("Error pretty-printing JSON: %w", err)
|
||||
}
|
||||
secretStr = strings.Split(dst.String(), "\n")
|
||||
if len(secretStr) > 0 {
|
||||
// Indent doesn't apply to the first line 🙄
|
||||
secretStr[0] = fmt.Sprintf(" %s", secretStr[0])
|
||||
}
|
||||
}
|
||||
default:
|
||||
// If it's not Vault, and not another known type,
|
||||
|
||||
Reference in new issue
Block a user