github.com/myhau/pulumi/pkg/v3@v3.70.2-0.20221116134521-f2775972e587/secrets/manager.go (about)

     1  // Copyright 2016-2018, Pulumi Corporation.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package secrets defines the interface common to all secret managers.
    16  package secrets
    17  
    18  import (
    19  	"encoding/json"
    20  
    21  	"github.com/pulumi/pulumi/sdk/v3/go/common/resource/config"
    22  )
    23  
    24  // Manager provides the interface for providing stack encryption.
    25  type Manager interface {
    26  	// Type retruns a string that reflects the type of this provider. This is serialized along with the state of
    27  	// the manager into the deployment such that we can re-construct the correct manager when deserializing a
    28  	// deployment into a snapshot.
    29  	Type() string
    30  	// An opaque state, which can be JSON serialized and used later to reconstruct the provider when deserializing
    31  	// the deployment into a snapshot.
    32  	State() interface{}
    33  	// Encrypter returns a `config.Encrypter` that can be used to encrypt values when serializing a snapshot into a
    34  	// deployment, or an error if one can not be constructed.
    35  	Encrypter() (config.Encrypter, error)
    36  	// Decrypter returns a `config.Decrypter` that can be used to decrypt values when deserializing a snapshot from a
    37  	// deployment, or an error if one can not be constructed.
    38  	Decrypter() (config.Decrypter, error)
    39  }
    40  
    41  // AreCompatible returns true if the two Managers are of the same type and have the same state.
    42  func AreCompatible(a, b Manager) bool {
    43  	if a == nil || b == nil {
    44  		return a == nil && b == nil
    45  	}
    46  
    47  	if a.Type() != b.Type() {
    48  		return false
    49  	}
    50  
    51  	as, err := json.Marshal(a.State())
    52  	if err != nil {
    53  		return false
    54  	}
    55  	bs, err := json.Marshal(b.State())
    56  	if err != nil {
    57  		return false
    58  	}
    59  	return string(as) == string(bs)
    60  }