Files
virtual-kubelet/vendor/github.com/vmware/vic/pkg/vsphere/extraconfig/secret.go
Loc Nguyen 513cebe7b7 VMware vSphere Integrated Containers provider (#206)
* Add Virtual Kubelet provider for VIC

Initial virtual kubelet provider for VMware VIC.  This provider currently
handles creating and starting of a pod VM via the VIC portlayer and persona
server.  Image store handling via the VIC persona server.  This provider
currently requires the feature/wolfpack branch of VIC.

* Added pod stop and delete.  Also added node capacity.

Added the ability to stop and delete pod VMs via VIC.  Also retrieve
node capacity information from the VCH.

* Cleanup and readme file

Some file clean up and added a Readme.md markdown file for the VIC
provider.

* Cleaned up errors, added function comments, moved operation code

1. Cleaned up error handling.  Set standard for creating errors.
2. Added method prototype comments for all interface functions.
3. Moved PodCreator, PodStarter, PodStopper, and PodDeleter to a new folder.

* Add mocking code and unit tests for podcache, podcreator, and podstarter

Used the unit test framework used in VIC to handle assertions in the provider's
unit test.  Mocking code generated using OSS project mockery, which is compatible
with the testify assertion framework.

* Vendored packages for the VIC provider

Requires feature/wolfpack branch of VIC and a few specific commit sha of
projects used within VIC.

* Implementation of POD Stopper and Deleter unit tests (#4)

* Updated files for initial PR
2018-06-04 15:41:32 -07:00

128 lines
3.1 KiB
Go

// Copyright 2016-2017 VMware, Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package extraconfig
import (
"crypto/rand"
"encoding/base64"
"errors"
"fmt"
log "github.com/Sirupsen/logrus"
"golang.org/x/crypto/nacl/secretbox"
)
// The value of this key is hidden from API requests, but visible within the guest
// #nosec: Potential hardcoded credentials
const GuestInfoSecretKey = "guestinfo.ovfEnv"
// SecretKey provides helpers to encrypt/decrypt extraconfig values
type SecretKey struct {
key [32]byte
}
// NewSecretKey generates a new secret key
func NewSecretKey() (*SecretKey, error) {
s := new(SecretKey)
if _, err := rand.Read(s.key[:]); err != nil {
return nil, err
}
return s, nil
}
// FromString base64 decodes an existing SecretKey
func (s *SecretKey) FromString(key string) error {
b, err := base64.StdEncoding.DecodeString(key)
if err != nil {
return err
}
if len(b) != 32 {
return errors.New("invalid secret key")
}
copy(s.key[:], b)
return nil
}
// String base64 encodes a SecretKey
func (s *SecretKey) String() string {
return base64.StdEncoding.EncodeToString(s.key[:])
}
// Source wraps the given DataSource, decrypting any secret values
func (s *SecretKey) Source(ds DataSource) DataSource {
// If GuestInfoSecretKey has a value, it should be our secret key.
// #nosec: Errors unhandled.
if val, _ := ds(GuestInfoSecretKey); val != "" {
if err := s.FromString(val); err != nil {
log.Errorf("failed to decode %s: %s", GuestInfoSecretKey, err)
} else {
log.Debugf("secret key decoded from %s", GuestInfoSecretKey)
}
}
return func(key string) (string, error) {
val, err := ds(key)
if err == nil && isSecret(key) {
b, err := base64.StdEncoding.DecodeString(val)
if err != nil {
return "", err
}
var nonce [24]byte
copy(nonce[:], b[:24])
plaintext, ok := secretbox.Open([]byte{}, b[24:], &nonce, &s.key)
if !ok {
return "", fmt.Errorf("failed to decrypt value for %s", key)
}
val = string(plaintext)
}
return val, err
}
}
// Sink wraps the given DataSink, encrypting any secret values
func (s *SecretKey) Sink(ds DataSink) DataSink {
// Store our secret key.
if err := ds(GuestInfoSecretKey, s.String()); err != nil {
log.Errorf("failed to store %s: %s", GuestInfoSecretKey, err)
}
return func(key, value string) error {
if isSecret(key) {
var nonce [24]byte
if _, err := rand.Read(nonce[:]); err != nil {
return err
}
ciphertext := secretbox.Seal(nonce[:], []byte(value), &nonce, &s.key)
value = base64.StdEncoding.EncodeToString(ciphertext)
}
return ds(key, value)
}
}