2015-08-19 06:59:30 +00:00
|
|
|
package secure
|
|
|
|
|
|
|
|
import (
|
2015-08-20 04:24:32 +00:00
|
|
|
"crypto/rsa"
|
2015-08-19 14:11:24 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"hash"
|
2015-08-19 06:59:30 +00:00
|
|
|
|
|
|
|
"github.com/drone/drone/Godeps/_workspace/src/gopkg.in/yaml.v2"
|
2015-08-19 14:11:24 +00:00
|
|
|
|
|
|
|
"github.com/drone/drone/pkg/utils/sshutil"
|
2015-08-19 06:59:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Parse parses and returns the secure section of the
|
|
|
|
// yaml file as plaintext parameters.
|
2015-08-20 04:24:32 +00:00
|
|
|
func Parse(privateKeyPEM, repoHash, raw string) (map[string]string, error) {
|
2015-08-19 06:59:30 +00:00
|
|
|
params, err := parseSecure(raw)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-08-20 04:24:32 +00:00
|
|
|
hasher := ToHash(repoHash)
|
|
|
|
privKey := sshutil.UnMarshalPrivateKey([]byte(privateKeyPEM))
|
|
|
|
|
|
|
|
err = DecryptMap(hasher, privKey, params)
|
2015-08-19 14:11:24 +00:00
|
|
|
return params, err
|
2015-08-19 06:59:30 +00:00
|
|
|
}
|
|
|
|
|
2015-08-19 14:11:24 +00:00
|
|
|
// DecryptMap decrypts values of a map of named parameters
|
|
|
|
// from base64 to decrypted strings.
|
2015-08-20 04:24:32 +00:00
|
|
|
func DecryptMap(hasher hash.Hash, privKey *rsa.PrivateKey, params map[string]string) error {
|
2015-08-19 06:59:30 +00:00
|
|
|
var err error
|
2015-08-19 14:11:24 +00:00
|
|
|
|
|
|
|
for name, encrypted := range params {
|
|
|
|
params[name], err = sshutil.Decrypt(hasher, privKey, encrypted)
|
2015-08-19 06:59:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-19 14:11:24 +00:00
|
|
|
// EncryptMap encrypts values of a map of named parameters
|
2015-08-20 04:24:32 +00:00
|
|
|
func EncryptMap(hasher hash.Hash, pubKey *rsa.PublicKey, params map[string]string) error {
|
2015-08-19 06:59:30 +00:00
|
|
|
var err error
|
2015-08-19 14:11:24 +00:00
|
|
|
|
2015-08-19 06:59:30 +00:00
|
|
|
for name, value := range params {
|
2015-08-20 04:24:32 +00:00
|
|
|
params[name], err = sshutil.Encrypt(hasher, pubKey, value)
|
2015-08-19 06:59:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-19 14:11:24 +00:00
|
|
|
// parseSecure is helper function to parse the Secure data from
|
2015-08-19 06:59:30 +00:00
|
|
|
// the raw yaml file.
|
|
|
|
func parseSecure(raw string) (map[string]string, error) {
|
|
|
|
data := struct {
|
|
|
|
Secure map[string]string
|
|
|
|
}{}
|
|
|
|
err := yaml.Unmarshal([]byte(raw), &data)
|
2015-08-19 14:11:24 +00:00
|
|
|
|
2015-08-19 06:59:30 +00:00
|
|
|
return data.Secure, err
|
|
|
|
}
|
2015-08-19 14:11:24 +00:00
|
|
|
|
2015-08-20 04:24:32 +00:00
|
|
|
// ToHash is helper function to generate Hash of given string
|
|
|
|
func ToHash(key string) hash.Hash {
|
2015-08-19 14:11:24 +00:00
|
|
|
hasher := sha256.New()
|
|
|
|
hasher.Write([]byte(key))
|
|
|
|
hasher.Reset()
|
|
|
|
return hasher
|
|
|
|
}
|