2019-02-26 14:41:02 +00:00
|
|
|
package oraclecloud
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rsa"
|
|
|
|
"encoding/base64"
|
2024-04-14 21:27:26 +00:00
|
|
|
"errors"
|
2019-02-26 14:41:02 +00:00
|
|
|
"fmt"
|
2019-03-05 18:57:04 +00:00
|
|
|
"os"
|
2019-02-26 14:41:02 +00:00
|
|
|
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/platform/config/env"
|
2024-04-14 21:27:26 +00:00
|
|
|
"github.com/oracle/oci-go-sdk/v65/common"
|
2019-02-26 14:41:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type configProvider struct {
|
|
|
|
values map[string]string
|
|
|
|
privateKeyPassphrase string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newConfigProvider(values map[string]string) *configProvider {
|
|
|
|
return &configProvider{
|
|
|
|
values: values,
|
2020-03-11 22:51:10 +00:00
|
|
|
privateKeyPassphrase: env.GetOrFile(EnvPrivKeyPass),
|
2019-02-26 14:41:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *configProvider) PrivateRSAKey() (*rsa.PrivateKey, error) {
|
2020-03-11 22:51:10 +00:00
|
|
|
privateKey, err := getPrivateKey(envPrivKey)
|
2019-02-26 14:41:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-02-09 20:55:43 +00:00
|
|
|
return common.PrivateKeyFromBytesWithPassword(privateKey, []byte(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) {
|
2020-03-11 22:51:10 +00:00
|
|
|
return p.values[EnvTenancyOCID], nil
|
2019-02-26 14:41:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *configProvider) UserOCID() (string, error) {
|
2020-03-11 22:51:10 +00:00
|
|
|
return p.values[EnvUserOCID], nil
|
2019-02-26 14:41:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *configProvider) KeyFingerprint() (string, error) {
|
2020-03-11 22:51:10 +00:00
|
|
|
return p.values[EnvPubKeyFingerprint], nil
|
2019-02-26 14:41:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *configProvider) Region() (string, error) {
|
2020-03-11 22:51:10 +00:00
|
|
|
return p.values[EnvRegion], nil
|
2019-02-26 14:41:02 +00:00
|
|
|
}
|
2019-03-05 18:57:04 +00:00
|
|
|
|
2024-04-14 21:27:26 +00:00
|
|
|
func (p *configProvider) AuthType() (common.AuthConfig, error) {
|
|
|
|
// Inspired by https://github.com/oracle/oci-go-sdk/blob/e7635c292e60d0a9dcdd3a1e7de180d7c99b1eee/common/configuration.go#L231-L234
|
|
|
|
return common.AuthConfig{AuthType: common.UnknownAuthenticationType}, errors.New("unsupported, keep the interface")
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("failed to read base64 value %s (defined by env var %s): %w", envVarValue, envVar, err)
|
2019-03-05 18:57:04 +00:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-08-25 09:44:11 +00:00
|
|
|
fileContents, err := os.ReadFile(fileVarValue)
|
2019-03-05 18:57:04 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("failed to read the file %s (defined by env var %s): %w", fileVarValue, fileVar, err)
|
2019-03-05 18:57:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fileContents, nil
|
|
|
|
}
|