github.com/gagliardetto/solana-go@v1.11.0/vault/secretboxer.go (about)

     1  // Copyright 2020 dfuse Platform Inc.
     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 vault
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  
    21  	"github.com/gagliardetto/solana-go/cli"
    22  	"github.com/pkg/errors"
    23  )
    24  
    25  type SecretBoxer interface {
    26  	Seal(in []byte) (string, error)
    27  	Open(in string) ([]byte, error)
    28  	WrapType() string
    29  }
    30  
    31  func SecretBoxerForType(boxerType string, keypath string) (SecretBoxer, error) {
    32  	switch boxerType {
    33  	case "kms-gcp":
    34  		if keypath == "" {
    35  			return nil, errors.New("missing kms-gcp keypath")
    36  		}
    37  		return NewKMSGCPBoxer(keypath), nil
    38  	case "passphrase":
    39  		var password string
    40  		var err error
    41  		if envVal := os.Getenv("SLNC_GLOBAL_INSECURE_VAULT_PASSPHRASE"); envVal != "" {
    42  			password = envVal
    43  		} else {
    44  			password, err = cli.GetDecryptPassphrase()
    45  			if err != nil {
    46  				return nil, err
    47  			}
    48  		}
    49  
    50  		return NewPassphraseBoxer(password), nil
    51  	default:
    52  		return nil, fmt.Errorf("unknown secret boxer: %s", boxerType)
    53  	}
    54  }