Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix illegal cast in resource constructors when secret-wrapping input arguments. #11673

Merged
merged 1 commit into from Dec 19, 2022

Conversation

AaronFriel
Copy link
Member

@AaronFriel AaronFriel commented Dec 17, 2022

Fixes #11664 after new SDK gen. Will require an update to pulumi-aws using this branch or a new release.

Codegen for wrapping input properties as secrets performed an incorrect cast, as seen in Pulumi's AWS classic SDK.

Using the sample program and the resource constructor described in #11664 as our test case, from pulumi-aws/sdk/v5/go/aws/secretmanager/secretVersion.go:

      func NewSecretVersion(ctx *pulumi.Context,
        name string, args *SecretVersionArgs, opts ...pulumi.ResourceOption) (*SecretVersion, error)
        if args == nil {
          return nil, errors.New("missing one or more required arguments")
        }

        if args.SecretId == nil {
          return nil, errors.New("invalid value for required argument 'SecretId'")
        }
        if args.SecretBinary != nil {
82:        args.SecretBinary = pulumi.ToSecret(args.SecretBinary).(pulumi.StringPtrOutput)
        }
        if args.SecretString != nil {
85:        args.SecretString = pulumi.ToSecret(args.SecretString).(pulumi.StringPtrOutput)
        }

args.SecretBinary is of type pulumi.StringPtrInput and pulumi.ToSecret returns pulumi.Output as an Output-wrapped value marked secret.

As StringPtrInput is an interface accepting multiple input types, the return value would be either pulumi.StringOutput or pulumi.StringPtrOutput. These are both concrete types, and casting to the incorrect one would panic.

Fortunately we can cast back to the input type, as verified by building the new codegen and testing the Pulumi program in #11664.

The new codegen below converts an input type T to pulumi.Output, then casts back to T.

      func NewSecretVersion(ctx *pulumi.Context,
        // ...
        if args.SecretBinary != nil {
82:             args.SecretBinary = pulumi.ToSecret(args.SecretBinary).(pulumi.StringPtrInput)
        }
        if args.SecretString != nil {
85:             args.SecretString = pulumi.ToSecret(args.SecretString).(pulumi.StringPtrInput)
        }

@pulumi-bot
Copy link
Contributor

Changelog

[uncommitted] (2022-12-17)

Bug Fixes

  • [sdkgen/go] Illegal cast in resource constructors when secret-wrapping input arguments.
    #11673

…pping input arguments

Codegen for wrapping input properties as secrets performed an incorrect cast, as
seen in Pulumi's AWS classic SDK.

Using the sample program and the resource constructor described in #11664 as our
 test case, from `pulumi-aws/sdk/v5/go/aws/secretmanager/secretVersion.go`:

```go
      func NewSecretVersion(ctx *pulumi.Context,
        name string, args *SecretVersionArgs, opts ...pulumi.ResourceOption) (*SecretVersion, error) {
        if args == nil {
          return nil, errors.New("missing one or more required arguments")
        }

        if args.SecretId == nil {
          return nil, errors.New("invalid value for required argument 'SecretId'")
        }
        if args.SecretBinary != nil {
82:        args.SecretBinary = pulumi.ToSecret(args.SecretBinary).(pulumi.StringPtrOutput)
        }
        if args.SecretString != nil {
85:        args.SecretString = pulumi.ToSecret(args.SecretString).(pulumi.StringPtrOutput)
        }
```

`args.SecretBinary` is of type `pulumi.StringPtrInput` and `pulumi.ToSecret`
returns `pulumi.Output` returns its input as an Output-wrapped value marked
secret.

As `StringPtrInput` is an interface accepting multiple input types, the return
value would be either `pulumi.StringOutput` `pulumi.StringPtrOutput`. These are
both concrete types, and casting to the incorrect one would panic.

Fortunately we can cast back to the arg's type, as verified by building the new
codegen and testing the Pulumi program in #11664. This should handle regular
inputs and plain inputs.

The new codegen below converts an input type `T` to `pulumi.Output`, then casts
back to `T`.

```go
      func NewSecretVersion(ctx *pulumi.Context,
        // ...
        if args.SecretBinary != nil {
82:             args.SecretBinary = pulumi.ToSecret(args.SecretBinary).(pulumi.StringPtrInput)
        }
        if args.SecretString != nil {
85:             args.SecretString = pulumi.ToSecret(args.SecretString).(pulumi.StringPtrInput)
        }
```
@AaronFriel AaronFriel marked this pull request as ready for review December 18, 2022 21:03
Copy link
Member

@iwahbe iwahbe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@AaronFriel
Copy link
Member Author

bors merge

@bors
Copy link
Contributor

bors bot commented Dec 19, 2022

Build succeeded:

@bors bors bot merged commit cc5eae1 into master Dec 19, 2022
@bors bors bot deleted the friel/11664 branch December 19, 2022 16:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sdk/go/aws/secretsmanager/secretVersion.go#L82-L92 are incorrectly cast.
3 participants