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

Commits on Dec 17, 2022

  1. fix(sdkgen/go): illegal cast in resource constructors when secret-wra…

    …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 committed Dec 17, 2022
    Configuration menu
    Copy the full SHA
    695360a View commit details
    Browse the repository at this point in the history