github.com/drone/runner-go@v1.12.0/manifest/signature.go (about)

     1  // Copyright 2019 Drone.IO Inc. All rights reserved.
     2  // Use of this source code is governed by the Polyform License
     3  // that can be found in the LICENSE file.
     4  
     5  package manifest
     6  
     7  import (
     8  	"errors"
     9  
    10  	"github.com/buildkite/yaml"
    11  )
    12  
    13  var _ Resource = (*Signature)(nil)
    14  
    15  type (
    16  	// Signature is a resource that provides an hmac
    17  	// signature of combined resources. This signature
    18  	// can be used to validate authenticity and prevent
    19  	// tampering.
    20  	Signature struct {
    21  		Version string `json:"version,omitempty"`
    22  		Kind    string `json:"kind"`
    23  		Type    string `json:"type"`
    24  		Name    string `json:"name"`
    25  		Hmac    string `json:"hmac"`
    26  	}
    27  )
    28  
    29  func init() {
    30  	Register(signatureFunc)
    31  }
    32  
    33  func signatureFunc(r *RawResource) (Resource, bool, error) {
    34  	if r.Kind != KindSignature {
    35  		return nil, false, nil
    36  	}
    37  	out := new(Signature)
    38  	err := yaml.Unmarshal(r.Data, out)
    39  	return out, true, err
    40  }
    41  
    42  // GetVersion returns the resource version.
    43  func (s *Signature) GetVersion() string { return s.Version }
    44  
    45  // GetKind returns the resource kind.
    46  func (s *Signature) GetKind() string { return s.Kind }
    47  
    48  // GetType returns the resource type.
    49  func (s *Signature) GetType() string { return s.Type }
    50  
    51  // GetName returns the resource name.
    52  func (s *Signature) GetName() string { return s.Name }
    53  
    54  // Validate returns an error if the signature is invalid.
    55  func (s Signature) Validate() error {
    56  	if s.Hmac == "" {
    57  		return errors.New("yaml: invalid signature. missing hash")
    58  	}
    59  	return nil
    60  }