github.com/drone/runner-go@v1.12.0/manifest/secret.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 = (*Secret)(nil) 14 15 type ( 16 // Secret is a resource that provides encrypted data 17 // and pointers to external data (i.e. from vault). 18 Secret struct { 19 Version string `json:"version,omitempty"` 20 Kind string `json:"kind,omitempty"` 21 Type string `json:"type,omitempty"` 22 Name string `json:"name,omitempty"` 23 Data string `json:"data,omitempty"` 24 Get SecretGet `json:"get,omitempty"` 25 } 26 27 // SecretGet defines a request to get a secret from 28 // an external sevice at the specified path, or with the 29 // specified name. 30 SecretGet struct { 31 Path string `json:"path,omitempty"` 32 Name string `json:"name,omitempty"` 33 Key string `json:"key,omitempty"` 34 } 35 ) 36 37 func init() { 38 Register(secretFunc) 39 } 40 41 func secretFunc(r *RawResource) (Resource, bool, error) { 42 if r.Kind != KindSecret { 43 return nil, false, nil 44 } 45 out := new(Secret) 46 err := yaml.Unmarshal(r.Data, out) 47 return out, true, err 48 } 49 50 // GetVersion returns the resource version. 51 func (s *Secret) GetVersion() string { return s.Version } 52 53 // GetKind returns the resource kind. 54 func (s *Secret) GetKind() string { return s.Kind } 55 56 // GetType returns the resource type. 57 func (s *Secret) GetType() string { return s.Type } 58 59 // GetName returns the resource name. 60 func (s *Secret) GetName() string { return s.Name } 61 62 // Validate returns an error if the secret is invalid. 63 func (s *Secret) Validate() error { 64 if len(s.Data) == 0 && 65 len(s.Get.Path) == 0 && 66 len(s.Get.Name) == 0 { 67 return errors.New("yaml: invalid secret resource") 68 } 69 return nil 70 }