mirror of
				https://github.com/optim-enterprises-bv/vault.git
				synced 2025-10-31 18:48:08 +00:00 
			
		
		
		
	command/generate-root: support returning json format on decode and generate otp (#7870)
This commit is contained in:
		 Calvin Leung Huang
					Calvin Leung Huang
				
			
				
					committed by
					
						 GitHub
						GitHub
					
				
			
			
				
	
			
			
			 GitHub
						GitHub
					
				
			
						parent
						
							7dcb9d88a3
						
					
				
				
					commit
					c73a776ba2
				
			| @@ -242,7 +242,16 @@ func (c *OperatorGenerateRootCommand) Run(args []string) int { | ||||
| 	case c.flagGenerateOTP: | ||||
| 		otp, code := c.generateOTP(client, kind) | ||||
| 		if code == 0 { | ||||
| 			return PrintRaw(c.UI, otp) | ||||
| 			switch Format(c.UI) { | ||||
| 			case "", "table": | ||||
| 				return PrintRaw(c.UI, otp) | ||||
| 			default: | ||||
| 				status := map[string]interface{}{ | ||||
| 					"otp":        otp, | ||||
| 					"otp_length": len(otp), | ||||
| 				} | ||||
| 				return OutputData(c.UI, status) | ||||
| 			} | ||||
| 		} | ||||
| 		return code | ||||
| 	case c.flagDecode != "": | ||||
| @@ -332,6 +341,7 @@ func (c *OperatorGenerateRootCommand) decode(client *api.Client, encoded, otp st | ||||
| 		return 2 | ||||
| 	} | ||||
|  | ||||
| 	var token string | ||||
| 	switch status.OTPLength { | ||||
| 	case 0: | ||||
| 		// Backwards compat | ||||
| @@ -341,13 +351,12 @@ func (c *OperatorGenerateRootCommand) decode(client *api.Client, encoded, otp st | ||||
| 			return 1 | ||||
| 		} | ||||
|  | ||||
| 		token, err := uuid.FormatUUID(tokenBytes) | ||||
| 		uuidToken, err := uuid.FormatUUID(tokenBytes) | ||||
| 		if err != nil { | ||||
| 			c.UI.Error(fmt.Sprintf("Error formatting base64 token value: %s", err)) | ||||
| 			return 1 | ||||
| 		} | ||||
|  | ||||
| 		return PrintRaw(c.UI, strings.TrimSpace(token)) | ||||
| 		token = strings.TrimSpace(uuidToken) | ||||
|  | ||||
| 	default: | ||||
| 		tokenBytes, err := base64.RawStdEncoding.DecodeString(encoded) | ||||
| @@ -361,8 +370,17 @@ func (c *OperatorGenerateRootCommand) decode(client *api.Client, encoded, otp st | ||||
| 			c.UI.Error(errwrap.Wrapf("Error xoring token: {{err}}", err).Error()) | ||||
| 			return 1 | ||||
| 		} | ||||
| 		token = string(tokenBytes) | ||||
| 	} | ||||
|  | ||||
| 		return PrintRaw(c.UI, string(tokenBytes)) | ||||
| 	switch Format(c.UI) { | ||||
| 	case "", "table": | ||||
| 		return PrintRaw(c.UI, token) | ||||
| 	default: | ||||
| 		tokenJSON := map[string]interface{}{ | ||||
| 			"token": token, | ||||
| 		} | ||||
| 		return OutputData(c.UI, tokenJSON) | ||||
| 	} | ||||
| } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user