2019-02-26 14:41:02 +00:00
|
|
|
package oraclecloud
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rsa"
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
2019-03-05 18:57:04 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2019-02-26 14:41:02 +00:00
|
|
|
|
2019-03-11 16:56:48 +00:00
|
|
|
"github.com/go-acme/lego/platform/config/env"
|
2019-02-26 14:41:02 +00:00
|
|
|
"github.com/oracle/oci-go-sdk/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-03-05 18:57:04 +00:00
|
|
|
ociPrivkey = "OCI_PRIVKEY"
|
2019-02-26 14:41:02 +00:00
|
|
|
ociPrivkeyPass = "OCI_PRIVKEY_PASS"
|
|
|
|
ociTenancyOCID = "OCI_TENANCY_OCID"
|
|
|
|
ociUserOCID = "OCI_USER_OCID"
|
|
|
|
ociPubkeyFingerprint = "OCI_PUBKEY_FINGERPRINT"
|
|
|
|
ociRegion = "OCI_REGION"
|
|
|
|
)
|
|
|
|
|
|
|
|
type configProvider struct {
|
|
|
|
values map[string]string
|
|
|
|
privateKeyPassphrase string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newConfigProvider(values map[string]string) *configProvider {
|
|
|
|
return &configProvider{
|
|
|
|
values: values,
|
|
|
|
privateKeyPassphrase: env.GetOrFile(ociPrivkeyPass),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *configProvider) PrivateRSAKey() (*rsa.PrivateKey, error) {
|
2019-03-05 18:57:04 +00:00
|
|
|
privateKey, err := getPrivateKey(ociPrivkey)
|
2019-02-26 14:41:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-03-05 18:57:04 +00:00
|
|
|
return common.PrivateKeyFromBytes(privateKey, common.String(p.privateKeyPassphrase))
|
2019-02-26 14:41:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *configProvider) KeyID() (string, error) {
|
|
|
|
tenancy, err := p.TenancyOCID()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := p.UserOCID()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
fingerprint, err := p.KeyFingerprint()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s/%s/%s", tenancy, user, fingerprint), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *configProvider) TenancyOCID() (value string, err error) {
|
|
|
|
return p.values[ociTenancyOCID], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *configProvider) UserOCID() (string, error) {
|
|
|
|
return p.values[ociUserOCID], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *configProvider) KeyFingerprint() (string, error) {
|
|
|
|
return p.values[ociPubkeyFingerprint], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *configProvider) Region() (string, error) {
|
|
|
|
return p.values[ociRegion], nil
|
|
|
|
}
|
2019-03-05 18:57:04 +00:00
|
|
|
|
|
|
|
func getPrivateKey(envVar string) ([]byte, error) {
|
|
|
|
envVarValue := os.Getenv(envVar)
|
|
|
|
if envVarValue != "" {
|
|
|
|
bytes, err := base64.StdEncoding.DecodeString(envVarValue)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to read base64 value %s (defined by env var %s): %s", envVarValue, envVar, err)
|
|
|
|
}
|
|
|
|
return bytes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
fileVar := envVar + "_FILE"
|
|
|
|
fileVarValue := os.Getenv(fileVar)
|
|
|
|
if fileVarValue == "" {
|
|
|
|
return nil, fmt.Errorf("no value provided for: %s or %s", envVar, fileVar)
|
|
|
|
}
|
|
|
|
|
|
|
|
fileContents, err := ioutil.ReadFile(fileVarValue)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to read the file %s (defined by env var %s): %s", fileVarValue, fileVar, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fileContents, nil
|
|
|
|
}
|