github.com/myhau/pulumi/pkg/v3@v3.70.2-0.20221116134521-f2775972e587/resource/deploy/manifest.go (about)

     1  // Copyright 2016-2022, 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 deploy
    16  
    17  import (
    18  	"crypto/sha256"
    19  	"fmt"
    20  	"time"
    21  
    22  	"github.com/blang/semver"
    23  	"github.com/pulumi/pulumi/sdk/v3/go/common/apitype"
    24  	"github.com/pulumi/pulumi/sdk/v3/go/common/workspace"
    25  )
    26  
    27  // Manifest captures versions for all binaries used to construct this snapshot.
    28  type Manifest struct {
    29  	Time    time.Time              // the time this snapshot was taken.
    30  	Magic   string                 // a magic cookie.
    31  	Version string                 // the pulumi command version.
    32  	Plugins []workspace.PluginInfo // the plugin versions also loaded.
    33  }
    34  
    35  // Serialize turns a manifest into a data structure suitable for serialization.
    36  func (m Manifest) Serialize() apitype.ManifestV1 {
    37  	manifest := apitype.ManifestV1{
    38  		Time:    m.Time,
    39  		Magic:   m.Magic,
    40  		Version: m.Version,
    41  	}
    42  	for _, plug := range m.Plugins {
    43  		var version string
    44  		if plug.Version != nil {
    45  			version = plug.Version.String()
    46  		}
    47  		manifest.Plugins = append(manifest.Plugins, apitype.PluginInfoV1{
    48  			Name:    plug.Name,
    49  			Path:    plug.Path,
    50  			Type:    plug.Kind,
    51  			Version: version,
    52  		})
    53  	}
    54  	return manifest
    55  }
    56  
    57  // DeserializeManifest deserializes a typed ManifestV1 into a `deploy.Manifest`.
    58  func DeserializeManifest(m apitype.ManifestV1) (*Manifest, error) {
    59  	manifest := Manifest{
    60  		Time:    m.Time,
    61  		Magic:   m.Magic,
    62  		Version: m.Version,
    63  	}
    64  	for _, plug := range m.Plugins {
    65  		var version *semver.Version
    66  		if v := plug.Version; v != "" {
    67  			sv, err := semver.ParseTolerant(v)
    68  			if err != nil {
    69  				return nil, err
    70  			}
    71  			version = &sv
    72  		}
    73  		manifest.Plugins = append(manifest.Plugins, workspace.PluginInfo{
    74  			Name:    plug.Name,
    75  			Kind:    plug.Type,
    76  			Version: version,
    77  		})
    78  	}
    79  	return &manifest, nil
    80  }
    81  
    82  // NewMagic creates a magic cookie out of a manifest; this can be used to check for tampering.  This ignores
    83  // any existing magic value already stored on the manifest.
    84  func (m Manifest) NewMagic() string {
    85  	if m.Version == "" {
    86  		return ""
    87  	}
    88  	return fmt.Sprintf("%x", sha256.Sum256([]byte(m.Version)))
    89  }