mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-09-22 13:39:35 +00:00
v1 crud secrets complete
This commit is contained in:
@@ -35,6 +35,12 @@ var secretsCmd = &cobra.Command{
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
workspaceFileExists := util.WorkspaceConfigFileExistsInCurrentPath()
|
||||||
|
if !workspaceFileExists {
|
||||||
|
log.Error("You have not yet connected to an Infisical Project. Please run [infisical init]")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
secrets, err := util.GetAllEnvironmentVariables("", environmentName)
|
secrets, err := util.GetAllEnvironmentVariables("", environmentName)
|
||||||
secrets = util.SubstituteSecrets(secrets)
|
secrets = util.SubstituteSecrets(secrets)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -57,24 +63,24 @@ var secretsGetCmd = &cobra.Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
var secretsSetCmd = &cobra.Command{
|
var secretsSetCmd = &cobra.Command{
|
||||||
Example: `secrets set <secret name A> <secret value A> <secret name B> <secret value B>..."`,
|
Example: `secrets set <secretName=secretValue> <secretName=secretValue>..."`,
|
||||||
Short: "Used update retrieve secrets by name",
|
Short: "Used set secrets",
|
||||||
Use: "set [secrets]",
|
Use: "set [secrets]",
|
||||||
DisableFlagsInUseLine: true,
|
DisableFlagsInUseLine: true,
|
||||||
PreRun: toggleDebug,
|
PreRun: toggleDebug,
|
||||||
Args: cobra.MinimumNArgs(1),
|
Args: cobra.MinimumNArgs(1),
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
secretType, err := cmd.Flags().GetString("type")
|
// secretType, err := cmd.Flags().GetString("type")
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
log.Errorln("Unable to parse the secret type flag")
|
// log.Errorln("Unable to parse the secret type flag")
|
||||||
log.Debugln(err)
|
// log.Debugln(err)
|
||||||
return
|
// return
|
||||||
}
|
// }
|
||||||
|
|
||||||
if !util.IsSecretTypeValid(secretType) {
|
// if !util.IsSecretTypeValid(secretType) {
|
||||||
log.Errorf("secret type can only be `personal` or `shared`. You have entered [%v]", secretType)
|
// log.Errorf("secret type can only be `personal` or `shared`. You have entered [%v]", secretType)
|
||||||
return
|
// return
|
||||||
}
|
// }
|
||||||
|
|
||||||
environmentName, err := cmd.Flags().GetString("env")
|
environmentName, err := cmd.Flags().GetString("env")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -91,6 +97,7 @@ var secretsSetCmd = &cobra.Command{
|
|||||||
workspaceFileExists := util.WorkspaceConfigFileExistsInCurrentPath()
|
workspaceFileExists := util.WorkspaceConfigFileExistsInCurrentPath()
|
||||||
if !workspaceFileExists {
|
if !workspaceFileExists {
|
||||||
log.Error("You have not yet connected to an Infisical Project. Please run [infisical init]")
|
log.Error("You have not yet connected to an Infisical Project. Please run [infisical init]")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
workspaceFile, err := util.GetWorkSpaceFromFile()
|
workspaceFile, err := util.GetWorkSpaceFromFile()
|
||||||
@@ -144,8 +151,15 @@ var secretsSetCmd = &cobra.Command{
|
|||||||
log.Debug(err)
|
log.Debug(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SecretSetOperation struct {
|
||||||
|
SecretKey string
|
||||||
|
SecretValue string
|
||||||
|
SecretOperation string
|
||||||
|
}
|
||||||
|
|
||||||
secretsToCreate := []models.Secret{}
|
secretsToCreate := []models.Secret{}
|
||||||
secretsToModify := []models.Secret{}
|
secretsToModify := []models.Secret{}
|
||||||
|
secretOperations := []SecretSetOperation{}
|
||||||
|
|
||||||
secretByKey := getSecretsByKeys(secrets)
|
secretByKey := getSecretsByKeys(secrets)
|
||||||
|
|
||||||
@@ -161,7 +175,8 @@ var secretsSetCmd = &cobra.Command{
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
key := splitKeyValueFromArg[0]
|
// Key and value from argument
|
||||||
|
key := strings.ToUpper(splitKeyValueFromArg[0])
|
||||||
value := splitKeyValueFromArg[1]
|
value := splitKeyValueFromArg[1]
|
||||||
|
|
||||||
hashedKey := fmt.Sprintf("%x", sha256.Sum256([]byte(key)))
|
hashedKey := fmt.Sprintf("%x", sha256.Sum256([]byte(key)))
|
||||||
@@ -189,6 +204,18 @@ var secretsSetCmd = &cobra.Command{
|
|||||||
// Only add to modifications if the value is different
|
// Only add to modifications if the value is different
|
||||||
if existingSecret.Value != value {
|
if existingSecret.Value != value {
|
||||||
secretsToModify = append(secretsToModify, encryptedSecretDetails)
|
secretsToModify = append(secretsToModify, encryptedSecretDetails)
|
||||||
|
secretOperations = append(secretOperations, SecretSetOperation{
|
||||||
|
SecretKey: key,
|
||||||
|
SecretValue: value,
|
||||||
|
SecretOperation: "SECRET VALUE MODIFIED",
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
// Current value is same as exisitng so no change
|
||||||
|
secretOperations = append(secretOperations, SecretSetOperation{
|
||||||
|
SecretKey: key,
|
||||||
|
SecretValue: value,
|
||||||
|
SecretOperation: "SECRET VALUE UNCHANGED",
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
@@ -202,9 +229,14 @@ var secretsSetCmd = &cobra.Command{
|
|||||||
SecretValueIV: base64.StdEncoding.EncodeToString(encryptedValue.Nonce),
|
SecretValueIV: base64.StdEncoding.EncodeToString(encryptedValue.Nonce),
|
||||||
SecretValueTag: base64.StdEncoding.EncodeToString(encryptedValue.AuthTag),
|
SecretValueTag: base64.StdEncoding.EncodeToString(encryptedValue.AuthTag),
|
||||||
SecretValueHash: hashedValue,
|
SecretValueHash: hashedValue,
|
||||||
Type: secretType,
|
Type: util.SECRET_TYPE_SHARED,
|
||||||
}
|
}
|
||||||
secretsToCreate = append(secretsToCreate, encryptedSecretDetails)
|
secretsToCreate = append(secretsToCreate, encryptedSecretDetails)
|
||||||
|
secretOperations = append(secretOperations, SecretSetOperation{
|
||||||
|
SecretKey: key,
|
||||||
|
SecretValue: value,
|
||||||
|
SecretOperation: "SECRET CREATED",
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -236,7 +268,14 @@ var secretsSetCmd = &cobra.Command{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infoln("secrets have been successfully set")
|
// Print secret operations
|
||||||
|
headers := []string{"SECRET NAME", "SECRET VALUE", "STATUS"}
|
||||||
|
rows := [][]string{}
|
||||||
|
for _, secretOperation := range secretOperations {
|
||||||
|
rows = append(rows, []string{secretOperation.SecretKey, secretOperation.SecretValue, secretOperation.SecretOperation})
|
||||||
|
}
|
||||||
|
|
||||||
|
visualize.Table(headers, rows)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -274,6 +313,7 @@ var secretsDeleteCmd = &cobra.Command{
|
|||||||
workspaceFileExists := util.WorkspaceConfigFileExistsInCurrentPath()
|
workspaceFileExists := util.WorkspaceConfigFileExistsInCurrentPath()
|
||||||
if !workspaceFileExists {
|
if !workspaceFileExists {
|
||||||
log.Error("You have not yet connected to an Infisical Project. Please run [infisical init]")
|
log.Error("You have not yet connected to an Infisical Project. Please run [infisical init]")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
workspaceFile, err := util.GetWorkSpaceFromFile()
|
workspaceFile, err := util.GetWorkSpaceFromFile()
|
||||||
@@ -343,6 +383,12 @@ func getSecretsByNames(cmd *cobra.Command, args []string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
workspaceFileExists := util.WorkspaceConfigFileExistsInCurrentPath()
|
||||||
|
if !workspaceFileExists {
|
||||||
|
log.Error("You have not yet connected to an Infisical Project. Please run [infisical init]")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
secrets, err := util.GetAllEnvironmentVariables("", environmentName)
|
secrets, err := util.GetAllEnvironmentVariables("", environmentName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Unable to retrieve secrets. Run with -d to see full logs")
|
log.Error("Unable to retrieve secrets. Run with -d to see full logs")
|
||||||
|
|||||||
@@ -80,3 +80,23 @@ func CallGetEncryptedWorkspaceKey(httpClient *resty.Client, request models.GetEn
|
|||||||
|
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CallGetEncryptedSecretsByWorkspaceIdAndEnv(httpClient resty.Client, request models.GetSecretsByWorkspaceIdAndEnvironmentRequest) (models.PullSecretsResponse, error) {
|
||||||
|
var pullSecretsRequestResponse models.PullSecretsResponse
|
||||||
|
response, err := httpClient.
|
||||||
|
R().
|
||||||
|
SetQueryParam("environment", request.EnvironmentName).
|
||||||
|
SetQueryParam("channel", "cli").
|
||||||
|
SetResult(&pullSecretsRequestResponse).
|
||||||
|
Get(fmt.Sprintf("%v/v1/secret/%v", util.INFISICAL_URL, request.WorkspaceId))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return models.PullSecretsResponse{}, fmt.Errorf("CallGetEncryptedSecretsByWorkspaceIdAndEnv: Unable to complete api request [err=%s]", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if response.StatusCode() > 299 {
|
||||||
|
return models.PullSecretsResponse{}, fmt.Errorf("CallGetEncryptedSecretsByWorkspaceIdAndEnv: Unsuccessful response: [response=%s]", response)
|
||||||
|
}
|
||||||
|
|
||||||
|
return pullSecretsRequestResponse, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -191,3 +191,8 @@ type GetEncryptedWorkspaceKeyResponse struct {
|
|||||||
UpdatedAt time.Time `json:"updatedAt"`
|
UpdatedAt time.Time `json:"updatedAt"`
|
||||||
} `json:"latestKey"`
|
} `json:"latestKey"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type GetSecretsByWorkspaceIdAndEnvironmentRequest struct {
|
||||||
|
EnvironmentName string `json:"environmentName"`
|
||||||
|
WorkspaceId string `json:"workspaceId"`
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ func PrintAllSecretDetails(secrets []models.SingleEnvironmentVariable) {
|
|||||||
rows = append(rows, []string{secret.Key, secret.Value, secret.Type})
|
rows = append(rows, []string{secret.Key, secret.Value, secret.Type})
|
||||||
}
|
}
|
||||||
|
|
||||||
headers := []string{"Secret name", "Secret vaule", "Secret type"}
|
headers := []string{"SECRET NAME", "SECRET VALUE", "SECRET TYPE"}
|
||||||
|
|
||||||
Table(headers, rows)
|
Table(headers, rows)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user