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 panics in applyTags when tags are unknown #2776

Merged
merged 6 commits into from
Sep 2, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 121 additions & 0 deletions provider/pvutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Copyright 2016-2023, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package provider

import (
"github.com/pulumi/pulumi-terraform-bridge/v3/unstable/propertyvalue"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
)

// Assits building transformations on PropertyValue that do not want to deal with Secret, Computed,
// or Output values.
//
// The usage pattern looks like this:
//
// composePropertyValue[T](func (c *pvComposer) (resource.PropertyValue, T, error) {
// x, _ := c.Simplify(pv1)
// y, _ := c.SimplifyPropertyMap(pv2) // etc
// return resource.NewArrayProperty([]resource.PropertyValue{x, y}), result, nil
// })
//
// User code accessing values that passed through Simplify is guaranteed to never observe Secret,
// Computed, or Output values. All the metadata bits about these is floated to top-level and
// re-applied to the value the user code receives out of composePropertyValue.
Copy link
Member

Choose a reason for hiding this comment

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

I think this is fine. It means that tags: { "foo": "bar", "secret": [secret] } will come out as [secret]. I doubt that this will be a problem in practice. I don't want to block merging on it.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm you're right. We're losing precision. Unlike what happens in the restricted CheckConfigure, there are true nested secrets here. That's bad especially when merging is not even involved, like empty provider tags + secret resource tags. Hm.. I may do a quick fix to this by keeping a track of secret string hashes and re-secreting matching in the output.

func composePropertyValue[T any](
f func(c *pvComposer) (resource.PropertyValue, T, error),
) (resource.PropertyValue, T, error) {
c := &pvComposer{}
v, r, err := f(c)
fv, err := c.finalize(v, err)
return fv, r, err
}

type pvComposer struct {
secret bool
deps []resource.URN
}

func (c *pvComposer) Simplify(
pv resource.PropertyValue,
) (resource.PropertyValue, error) {
return propertyvalue.TransformErr(c.simplifyOne, pv)
}

func (c *pvComposer) SimplifyPropertyMap(
pm resource.PropertyMap,
) (resource.PropertyMap, error) {
res := resource.PropertyMap{}
for k, v := range pm {
sv, err := c.Simplify(v)
if err != nil {
return nil, err
}
res[k] = sv
}
return res, nil
}

func (c *pvComposer) simplifyOne(
pv resource.PropertyValue,
) (resource.PropertyValue, error) {
for {
switch {
case pv.IsSecret():
pv = pv.SecretValue().Element
c.secret = true
case pv.IsComputed():
return resource.PropertyValue{}, &foundUnknownError{}
case pv.IsOutput():
if !pv.OutputValue().Known {
return resource.PropertyValue{}, &foundUnknownError{}
}
ov := pv.OutputValue()
c.secret = c.secret || ov.Secret
c.deps = append(c.deps, ov.Dependencies...)
pv = ov.Element
default:
return pv, nil
}
}
}

func (c *pvComposer) finalize(
pv resource.PropertyValue,
err error,
) (resource.PropertyValue, error) {
if _, unk := err.(*foundUnknownError); unk {
return resource.NewOutputProperty(resource.Output{
Known: false,
}), nil
}
if err != nil {
return pv, err
}
if c.deps != nil || c.secret {
return resource.NewOutputProperty(resource.Output{
Element: pv,
Known: true,
Secret: c.secret,
Dependencies: c.deps,
}), nil
}
return pv, nil
}

type foundUnknownError struct{}

func (m *foundUnknownError) Error() string {
return "foundUnknownError"
}
93 changes: 0 additions & 93 deletions provider/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -7152,96 +7152,3 @@ func Provider() *tfbridge.ProviderInfo {

return &prov
}

// Apply provider tags to an individual resource.
//
// Historically, Pulumi has struggles to handle the "tags" and "tags_all" fields correctly:
// - https://github.com/pulumi/pulumi-aws/issues/2633
// - https://github.com/pulumi/pulumi-aws/issues/1655
//
// terraform-provider-aws has also struggled with implementing their desired behavior:
// - https://github.com/hashicorp/terraform-provider-aws/issues/29747
// - https://github.com/hashicorp/terraform-provider-aws/issues/29842
// - https://github.com/hashicorp/terraform-provider-aws/issues/24449
//
// The Terraform lifecycle simply does not have a good way to map provider configuration
// onto resource values, so terraform-provider-aws is forced to work around limitations in
// unreliable ways. For example, terraform-provider-aws does not apply tags correctly with
// -refresh=false.
//
// This gives pulumi the same limitations by default. However, unlike Terraform, Pulumi
// does have a clear way to insert provider configuration into resource properties:
// Check. By writing a custom check function that applies "default_tags" to "tags" before
// the Terraform provider sees any resource configuration, we can give a consistent,
// reliable and good experience for Pulumi users.
func applyTags(
ctx context.Context, config resource.PropertyMap, meta resource.PropertyMap,
) (resource.PropertyMap, error) {
var defaultTags awsShim.TagConfig

unknown := func() (resource.PropertyMap, error) {
current := config["tags"]
if current.IsOutput() {
output := current.OutputValue()
output.Known = false
config["tags"] = resource.NewOutputProperty(output)
} else {
config["tags"] = resource.MakeOutput(current)
}
return config, nil
}

// awsShim.NewTagConfig accepts (context.Context, i interface{}) where i can be
// one of map[string]interface{} among other types. .Mappable() produces a
// map[string]interface{} where every value is of type string. This is well
// handled by awsShim.NewTagConfig.
//
// config values are guaranteed to be of the correct type because they have
// already been seen and approved of by the provider, which verifies its
// configuration is well typed.

if defaults, ok := meta["defaultTags"]; ok {
if defaults.ContainsUnknowns() {
return unknown()
}
if defaults.IsObject() {
defaults := defaults.ObjectValue()
tags, ok := defaults["tags"]
if ok {
defaultTags = awsShim.NewTagConfig(ctx, tags.Mappable())
}
}
}

ignoredTags := &awsShim.TagIgnoreConfig{}
if ignores, ok := meta["ignoreTags"]; ok {
if ignores.ContainsUnknowns() {
return unknown()
}
if keys, ok := ignores.ObjectValue()["keys"]; ok {
ignoredTags.Keys = awsShim.NewTagConfig(ctx, keys.Mappable()).Tags
}
if keys, ok := ignores.ObjectValue()["keyPrefixes"]; ok {
ignoredTags.KeyPrefixes = awsShim.NewTagConfig(ctx, keys.Mappable()).Tags
}
}

var resourceTags awsShim.TagConfig
if tags, ok := config["tags"]; ok {
resourceTags = awsShim.NewTagConfig(ctx, tags.Mappable().(map[string]interface{}))
}

allTags := defaultTags.MergeTags(resourceTags.Tags).IgnoreConfig(ignoredTags)

if len(allTags) > 0 {
allTagProperties := make(resource.PropertyMap, len(allTags))
for k, v := range allTags {
allTagProperties[resource.PropertyKey(k)] = resource.NewStringProperty(v.ValueString())
}
config["tags"] = resource.NewObjectProperty(allTagProperties)
} else {
delete(config, "tags")
}

return config, nil
}
135 changes: 135 additions & 0 deletions provider/tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// Copyright 2016-2023, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package provider

import (
"context"

awsShim "github.com/hashicorp/terraform-provider-aws/shim"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
)

// Apply provider tags to an individual resource.
//
// Historically, Pulumi has struggles to handle the "tags" and "tags_all" fields correctly:
// - https://github.com/pulumi/pulumi-aws/issues/2633
// - https://github.com/pulumi/pulumi-aws/issues/1655
//
// terraform-provider-aws has also struggled with implementing their desired behavior:
// - https://github.com/hashicorp/terraform-provider-aws/issues/29747
// - https://github.com/hashicorp/terraform-provider-aws/issues/29842
// - https://github.com/hashicorp/terraform-provider-aws/issues/24449
//
// The Terraform lifecycle simply does not have a good way to map provider configuration
// onto resource values, so terraform-provider-aws is forced to work around limitations in
// unreliable ways. For example, terraform-provider-aws does not apply tags correctly with
// -refresh=false.
//
// This gives pulumi the same limitations by default. However, unlike Terraform, Pulumi
// does have a clear way to insert provider configuration into resource properties:
// Check. By writing a custom check function that applies "default_tags" to "tags" before
// the Terraform provider sees any resource configuration, we can give a consistent,
// reliable and good experience for Pulumi users.
func applyTags(
ctx context.Context, config resource.PropertyMap, meta resource.PropertyMap,
) (resource.PropertyMap, error) {
ret := config.Copy()
configTags := resource.NewObjectProperty(resource.PropertyMap{})
if t, ok := config["tags"]; ok {
configTags = t
}
allTags, hasTags, err := mergeTags(ctx, configTags, meta)
if err != nil {
return nil, err
}
// If there are 0 tags, delete the tags entry rather than sending an empty map. The unknown
// case is quirky though, prefer to send the unknown marker out rather than deleting it.
if !hasTags && !allTags.ContainsUnknowns() {
Copy link
Member

Choose a reason for hiding this comment

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

I think this is equally correct, and simpler to comprehend. It allows us to remove hasTags.

Suggested change
if !hasTags && !allTags.ContainsUnknowns() {
if allTags.IsNil() && !allTags.ContainsUnknowns() {

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah but it also can be an Output{Element: nil, Dependencies}, among other things.

Copy link
Member Author

Choose a reason for hiding this comment

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

Well you're right. IsNil() is the only case we truly want to elide. The condition should just be a nil-check

delete(ret, "tags")
return ret, nil
}
ret["tags"] = allTags
return ret, nil
}

// Wrap mergeTagsSimple with taking care of unknowns, secrets and outputs.
func mergeTags(
ctx context.Context, tags resource.PropertyValue, meta resource.PropertyMap,
) (resource.PropertyValue, bool, error) {
return composePropertyValue(
func(c *pvComposer) (resource.PropertyValue, bool, error) {
stags, err := c.Simplify(tags)
if err != nil {
return resource.PropertyValue{}, false, err
}
smeta, err := c.SimplifyPropertyMap(meta)
if err != nil {
return resource.PropertyValue{}, false, err
}
return mergeTagsSimple(ctx, stags, smeta)
})
}

// At this level we do not need to track secret or unknown anymore.
func mergeTagsSimple(
ctx context.Context, tags resource.PropertyValue, meta resource.PropertyMap,
) (resource.PropertyValue, bool, error) {
var defaultTags awsShim.TagConfig

// awsShim.NewTagConfig accepts (context.Context, i interface{}) where i can be one of
// map[string]interface{} among other types. .Mappable() produces a map[string]interface{}
// where every value is of type string. This is well handled by awsShim.NewTagConfig.
//
// config values are guaranteed to be of the correct type because they have already been
// seen and approved of by the provider, which verifies its configuration is well typed.

if defaults, ok := meta["defaultTags"]; ok {
if defaults.IsObject() {
defaults := defaults.ObjectValue()
tags, ok := defaults["tags"]
if ok {
defaultTags = awsShim.NewTagConfig(ctx, tags.Mappable())
}
}
}

ignoredTags := &awsShim.TagIgnoreConfig{}
if ignores, ok := meta["ignoreTags"]; ok {
if keys, ok := ignores.ObjectValue()["keys"]; ok {
ignoredTags.Keys = awsShim.NewTagConfig(ctx, keys.Mappable()).Tags
}
if keys, ok := ignores.ObjectValue()["keyPrefixes"]; ok {
ignoredTags.KeyPrefixes = awsShim.NewTagConfig(ctx, keys.Mappable()).Tags
}
}

var resourceTags awsShim.TagConfig
if tags.IsObject() {
resourceTags = awsShim.NewTagConfig(ctx, tags.Mappable())
}

allTags := defaultTags.MergeTags(resourceTags.Tags).IgnoreConfig(ignoredTags)

if len(allTags) > 0 {
allTagProperties := make(resource.PropertyMap, len(allTags))
for k, v := range allTags {
pk := resource.PropertyKey(k)
allTagProperties[pk] = resource.NewStringProperty(v.ValueString())
}
return resource.NewObjectProperty(allTagProperties), true, nil
} else {
return resource.PropertyValue{}, false, nil
}
}
Loading
Loading