Skip to content

Commit

Permalink
fix(sdkgen/go): illegal cast in resource constructors when secret-wra…
Browse files Browse the repository at this point in the history
…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)
        }
```
  • Loading branch information
AaronFriel committed Dec 17, 2022
1 parent 184e579 commit 695360a
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 12 deletions.
@@ -0,0 +1,4 @@
changes:
- type: fix
scope: sdkgen/go
description: Illegal cast in resource constructors when secret-wrapping input arguments.
3 changes: 2 additions & 1 deletion pkg/codegen/go/gen.go
Expand Up @@ -1763,7 +1763,8 @@ func (pkg *pkgContext) genResource(w io.Writer, r *schema.Resource, generateReso
// Setup secrets
for _, p := range secretInputProps {
fmt.Fprintf(w, "\tif args.%s != nil {\n", pkg.fieldName(r, p))
fmt.Fprintf(w, "\t\targs.%[1]s = pulumi.ToSecret(args.%[1]s).(%[2]s)\n", pkg.fieldName(r, p), pkg.outputType(p.Type))

fmt.Fprintf(w, "\t\targs.%[1]s = pulumi.ToSecret(args.%[1]s).(%[2]s)\n", pkg.fieldName(r, p), pkg.typeString(p.Type))
fmt.Fprintf(w, "\t}\n")
}
if len(secretProps) > 0 {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions pkg/codegen/testing/test/testdata/secrets/go/mypkg/resource.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 695360a

Please sign in to comment.