Skip to content

Commit

Permalink
Merge pull request #2337 from posit-dev/dotnomad/secret-add
Browse files Browse the repository at this point in the history
Check if an environment variable exists when validating added secret
  • Loading branch information
dotNomad authored Oct 4, 2024
2 parents 0208b8f + 4ddbbf3 commit d88edab
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
19 changes: 17 additions & 2 deletions extensions/vscode/src/views/homeView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
isPreContentRecordWithConfig,
useApi,
AllContentRecordTypes,
EnvironmentConfig,
} from "src/api";
import { useBus } from "src/bus";
import { EventStream } from "src/events";
Expand Down Expand Up @@ -944,7 +945,16 @@ export class HomeViewProvider implements WebviewViewProvider, Disposable {
}
}

private inputSecretName = async () => {
private inputSecretName = async (
environment: EnvironmentConfig | undefined,
) => {
const existingKeys = new Set();
if (environment) {
for (const secret in environment) {
existingKeys.add(secret);
}
}

return await window.showInputBox({
title: "Add a Secret",
prompt: "Enter the name of the secret.",
Expand All @@ -953,6 +963,9 @@ export class HomeViewProvider implements WebviewViewProvider, Disposable {
if (value.length === 0) {
return "Secret names cannot be empty.";
}
if (existingKeys.has(value)) {
return "There is already an environment variable with this name. Secrets and environment variable names must be unique.";
}
return;
},
});
Expand All @@ -971,7 +984,9 @@ export class HomeViewProvider implements WebviewViewProvider, Disposable {
return;
}

const name = await this.inputSecretName();
const name = await this.inputSecretName(
activeConfig.configuration.environment,
);
if (name === undefined) {
// Cancelled by the user
return;
Expand Down
8 changes: 8 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
// Copyright (C) 2023 by Posit Software, PBC.

import (
"errors"
"fmt"
"io"
"strings"
Expand Down Expand Up @@ -114,6 +115,13 @@ func (cfg *Config) AddSecret(secret string) error {
return nil // Secret already exists, no need to add
}
}
// Check if the secret name already exists in the environment
for e := range cfg.Environment {
if e == secret {
return errors.New("secret name already exists in environment")
}
}

cfg.Secrets = append(cfg.Secrets, secret)
return nil
}
Expand Down
8 changes: 8 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@ func (s *ConfigSuite) TestApplySecretActionAddNoDuplicates() {
s.Equal([]string{"existingSecret1", "existingSecret2"}, cfg.Secrets)
}

func (s *ConfigSuite) TestApplySecretActionAddExitingEnvVar() {
cfg := New()
cfg.Environment = map[string]string{"existingEnvVar": "value"}
cfg.Secrets = []string{}
err := cfg.AddSecret("existingEnvVar")
s.NotNil(err)
}

func (s *ConfigSuite) TestApplySecretActionRemove() {
cfg := New()
cfg.Secrets = []string{"secret1", "secret2"}
Expand Down

0 comments on commit d88edab

Please sign in to comment.