# SecretStringValueBeta1

An experimental class used to specify an initial secret value for a Secret.

The class wraps a simple string (or JSON representation) in order to provide some safety checks and warnings
about the dangers of using plaintext strings as initial secret seed values via CDK/CloudFormation.

Obtainable from: SecretStringValueBeta1.fromToken, SecretStringValueBeta1.fromUnsafePlaintext

- Source: [`aws-secretsmanager/lib/secret.ts`](https://github.com/aws/aws-cdk/blob/v2.278.0/packages/aws-cdk-lib/aws-secretsmanager/lib/secret.ts#L261)
- Type last changed: v2.21.0 · 22 Apr 2022

## Example

TypeScript:

```ts
const user = new iam.User(this, 'User');
const accessKey = new iam.AccessKey(this, 'AccessKey', { user });
const secretValue = secretsmanager.SecretStringValueBeta1.fromToken(JSON.stringify({
  username: user.userName,
  database: 'foo',
  password: accessKey.secretAccessKey.unsafeUnwrap(),
}));
```

Python:

```python
user = iam.User(self, "User")
access_key = iam.AccessKey(self, "AccessKey", user=user)
secret_value = secretsmanager.SecretStringValueBeta1.from_token(JSON.stringify({
    "username": user.user_name,
    "database": "foo",
    "password": access_key.secret_access_key.unsafe_unwrap()
}))
```

Java:

```java
User user = new User(this, "User");
AccessKey accessKey = AccessKey.Builder.create(this, "AccessKey").user(user).build();
SecretStringValueBeta1 secretValue = SecretStringValueBeta1.fromToken(JSON.stringify(Map.of(
        "username", user.getUserName(),
        "database", "foo",
        "password", accessKey.secretAccessKey.unsafeUnwrap())));
```

C# / .NET:

```csharp
// Install: dotnet add package Amazon.CDK.Lib
var user = new User(this, "User");
var accessKey = new AccessKey(this, "AccessKey", new AccessKeyProps { User = user });
var secretValue = SecretStringValueBeta1.FromToken(JSON.Stringify(new Dictionary<string, object> {
    { "username", user.UserName },
    { "database", "foo" },
    { "password", accessKey.SecretAccessKey.UnsafeUnwrap() }
}));
```

Golang:

```go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
user := iam.NewUser(this, jsii.String("User"))
accessKey := iam.NewAccessKey(this, jsii.String("AccessKey"), &AccessKeyProps{
	User: User,
})
secretValue := secretsmanager.SecretStringValueBeta1_FromToken(jSON.stringify(map[string]interface{}{
	"username": user.userName,
	"database": jsii.String("foo"),
	"password": accessKey.secretAccessKey.unsafeUnwrap(),
}))
```

## Methods

- `fromToken(secretValueFromToken: string): SecretStringValueBeta1 // chainable` (static) — Creates a `SecretValueValueBeta1` from a string value coming from a Token.

  The intent is to enable creating secrets from references (e.g., `Ref`, `Fn::GetAtt`) from other resources.
  This might be the direct output of another Construct, or the output of a Custom Resource.
  This method throws if it determines the input is an unsafe plaintext string.

  For example:

  ```ts
  // Creates a new IAM user, access and secret keys, and stores the secret access key in a Secret.
  const user = new iam.User(this, 'User');
  const accessKey = new iam.AccessKey(this, 'AccessKey', { user });
  const secret = new secretsmanager.Secret(this, 'Secret', {
  	secretStringValue: accessKey.secretAccessKey,
  });
  ```

  The secret may also be embedded in a string representation of a JSON structure:

  ```ts
  const user = new iam.User(this, 'User');
  const accessKey = new iam.AccessKey(this, 'AccessKey', { user });
  const secretValue = secretsmanager.SecretStringValueBeta1.fromToken(JSON.stringify({
    username: user.userName,
    database: 'foo',
    password: accessKey.secretAccessKey.unsafeUnwrap(),
  }));
  ```

  Note that the value being a Token does *not* guarantee safety. For example, a Lazy-evaluated string
  (e.g., `Lazy.string({ produce: () => 'myInsecurePassword' }))`) is a Token, but as the output is
  ultimately a plaintext string, and so insecure.

  **Parameters**

  - `secretValueFromToken` — a secret value coming from a Construct attribute or Custom Resource output.

- `fromUnsafePlaintext(secretValue: string): SecretStringValueBeta1 // chainable` (static) — Creates a `SecretStringValueBeta1` from a plaintext value.

  This approach is inherently unsafe, as the secret value may be visible in your source control repository
  and will also appear in plaintext in the resulting CloudFormation template, including in the AWS Console or APIs.
  Usage of this method is discouraged, especially for production workloads.

- `secretValue(): string` — Returns the secret value.
