2017-09-12 11:30:39 -04:00
|
|
|
package oci
|
2017-02-13 05:35:14 -05:00
|
|
|
|
|
|
|
import (
|
2018-04-11 05:20:40 -04:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
2017-02-13 05:35:14 -05:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2018-04-11 05:20:40 -04:00
|
|
|
"github.com/go-ini/ini"
|
2017-02-13 05:35:14 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func testConfig(accessConfFile *os.File) map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
2020-03-15 13:22:06 -04:00
|
|
|
|
2017-02-13 05:35:14 -05:00
|
|
|
"availability_domain": "aaaa:PHX-AD-3",
|
|
|
|
"access_cfg_file": accessConfFile.Name(),
|
|
|
|
|
|
|
|
// Image
|
|
|
|
"base_image_ocid": "ocd1...",
|
|
|
|
"image_name": "HelloWorld",
|
|
|
|
|
|
|
|
// Networking
|
|
|
|
"subnet_ocid": "ocd1...",
|
2017-02-13 07:37:27 -05:00
|
|
|
|
|
|
|
// Comm
|
2018-02-13 08:20:26 -05:00
|
|
|
"ssh_username": "opc",
|
|
|
|
"use_private_ip": false,
|
2018-07-02 04:48:08 -04:00
|
|
|
"metadata": map[string]string{
|
|
|
|
"key": "value",
|
|
|
|
},
|
2019-09-27 08:49:37 -04:00
|
|
|
"defined_tags": map[string]map[string]interface{}{
|
|
|
|
"namespace": {"key": "value"},
|
|
|
|
},
|
2020-09-08 23:30:37 -04:00
|
|
|
|
|
|
|
// Instance Details
|
|
|
|
"instance_name": "hello-world",
|
|
|
|
"instance_tags": map[string]string{
|
|
|
|
"key": "value",
|
|
|
|
},
|
|
|
|
"create_vnic_details": map[string]interface{}{
|
|
|
|
"nsg_ids": []string{"ocd1..."},
|
|
|
|
},
|
|
|
|
"shape": "VM.Standard1.1",
|
2017-02-13 05:35:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfig(t *testing.T) {
|
2018-07-31 03:19:45 -04:00
|
|
|
// Shared set-up and deferred deletion
|
2017-02-13 05:35:14 -05:00
|
|
|
|
2018-04-11 05:20:40 -04:00
|
|
|
cfg, keyFile, err := baseTestConfigWithTmpKeyFile()
|
2017-02-13 05:35:14 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.Remove(keyFile.Name())
|
|
|
|
|
2018-04-11 05:20:40 -04:00
|
|
|
cfgFile, err := writeTestConfig(cfg)
|
2017-02-13 05:35:14 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.Remove(cfgFile.Name())
|
|
|
|
|
|
|
|
// Temporarily set $HOME to temp directory to bypass default
|
|
|
|
// access config loading.
|
|
|
|
|
|
|
|
tmpHome, err := ioutil.TempDir("", "packer_config_test")
|
|
|
|
if err != nil {
|
2018-04-11 05:20:40 -04:00
|
|
|
t.Fatalf("Unexpected error when creating temporary directory: %+v", err)
|
2017-02-13 05:35:14 -05:00
|
|
|
}
|
|
|
|
defer os.Remove(tmpHome)
|
|
|
|
|
|
|
|
home := os.Getenv("HOME")
|
|
|
|
os.Setenv("HOME", tmpHome)
|
|
|
|
defer os.Setenv("HOME", home)
|
|
|
|
|
|
|
|
// Config tests
|
|
|
|
t.Run("BaseConfig", func(t *testing.T) {
|
|
|
|
raw := testConfig(cfgFile)
|
2019-12-17 05:25:56 -05:00
|
|
|
var c Config
|
|
|
|
errs := c.Prepare(raw)
|
2017-02-13 05:35:14 -05:00
|
|
|
|
|
|
|
if errs != nil {
|
2018-04-11 05:20:40 -04:00
|
|
|
t.Fatalf("Unexpected error in configuration %+v", errs)
|
2017-02-13 05:35:14 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("NoAccessConfig", func(t *testing.T) {
|
|
|
|
raw := testConfig(cfgFile)
|
|
|
|
delete(raw, "access_cfg_file")
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
var c Config
|
|
|
|
errs := c.Prepare(raw)
|
2017-02-13 05:35:14 -05:00
|
|
|
|
|
|
|
expectedErrors := []string{
|
2018-04-11 05:20:40 -04:00
|
|
|
"'user_ocid'", "'tenancy_ocid'", "'fingerprint'", "'key_file'",
|
2017-02-13 05:35:14 -05:00
|
|
|
}
|
2018-04-11 05:20:40 -04:00
|
|
|
|
|
|
|
s := errs.Error()
|
2017-02-13 05:35:14 -05:00
|
|
|
for _, expected := range expectedErrors {
|
|
|
|
if !strings.Contains(s, expected) {
|
2018-04-11 05:20:40 -04:00
|
|
|
t.Errorf("Expected %q to contain '%s'", s, expected)
|
2017-02-13 05:35:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("AccessConfigTemplateOnly", func(t *testing.T) {
|
|
|
|
raw := testConfig(cfgFile)
|
|
|
|
delete(raw, "access_cfg_file")
|
|
|
|
raw["user_ocid"] = "ocid1..."
|
|
|
|
raw["tenancy_ocid"] = "ocid1..."
|
|
|
|
raw["fingerprint"] = "00:00..."
|
|
|
|
raw["key_file"] = keyFile.Name()
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
var c Config
|
|
|
|
errs := c.Prepare(raw)
|
2017-02-13 05:35:14 -05:00
|
|
|
|
|
|
|
if errs != nil {
|
2017-02-13 07:51:01 -05:00
|
|
|
t.Fatalf("err: %+v", errs)
|
2017-02-13 05:35:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("TenancyReadFromAccessCfgFile", func(t *testing.T) {
|
|
|
|
raw := testConfig(cfgFile)
|
2019-12-17 05:25:56 -05:00
|
|
|
var c Config
|
|
|
|
errs := c.Prepare(raw)
|
2017-02-13 05:35:14 -05:00
|
|
|
if errs != nil {
|
2018-04-11 05:20:40 -04:00
|
|
|
t.Fatalf("Unexpected error in configuration %+v", errs)
|
2017-02-13 05:35:14 -05:00
|
|
|
}
|
|
|
|
|
2019-10-14 10:08:16 -04:00
|
|
|
tenancy, err := c.configProvider.TenancyOCID()
|
2018-04-11 05:20:40 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error getting tenancy ocid: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := "ocid1.tenancy.oc1..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
|
|
|
if tenancy != expected {
|
|
|
|
t.Errorf("Expected tenancy: %s, got %s.", expected, tenancy)
|
2017-02-13 05:35:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
2017-09-29 05:51:31 -04:00
|
|
|
t.Run("RegionNotDefaultedToPHXWhenSetInOCISettings", func(t *testing.T) {
|
|
|
|
raw := testConfig(cfgFile)
|
2019-12-17 05:25:56 -05:00
|
|
|
var c Config
|
|
|
|
errs := c.Prepare(raw)
|
2017-09-29 05:51:31 -04:00
|
|
|
if errs != nil {
|
2018-04-11 05:20:40 -04:00
|
|
|
t.Fatalf("Unexpected error in configuration %+v", errs)
|
|
|
|
}
|
|
|
|
|
2019-10-14 10:08:16 -04:00
|
|
|
region, err := c.configProvider.Region()
|
2018-04-11 05:20:40 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unexpected error getting region: %v", err)
|
2017-09-29 05:51:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
expected := "us-ashburn-1"
|
2018-04-11 05:20:40 -04:00
|
|
|
if region != expected {
|
|
|
|
t.Errorf("Expected region: %s, got %s.", expected, region)
|
2017-09-29 05:51:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
2017-02-13 05:35:14 -05:00
|
|
|
// Test the correct errors are produced when required template keys are
|
|
|
|
// omitted.
|
2017-08-02 05:51:08 -04:00
|
|
|
requiredKeys := []string{"availability_domain", "base_image_ocid", "shape", "subnet_ocid"}
|
2017-02-13 05:35:14 -05:00
|
|
|
for _, k := range requiredKeys {
|
|
|
|
t.Run(k+"_required", func(t *testing.T) {
|
|
|
|
raw := testConfig(cfgFile)
|
|
|
|
delete(raw, k)
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
var c Config
|
|
|
|
errs := c.Prepare(raw)
|
2017-02-13 05:35:14 -05:00
|
|
|
|
|
|
|
if !strings.Contains(errs.Error(), k) {
|
|
|
|
t.Errorf("Expected '%s' to contain '%s'", errs.Error(), k)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-08-02 05:51:08 -04:00
|
|
|
t.Run("ImageNameDefaultedIfEmpty", func(t *testing.T) {
|
|
|
|
raw := testConfig(cfgFile)
|
|
|
|
delete(raw, "image_name")
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
var c Config
|
|
|
|
errs := c.Prepare(raw)
|
2017-08-02 05:51:08 -04:00
|
|
|
if errs != nil {
|
2018-04-11 05:20:40 -04:00
|
|
|
t.Fatalf("Unexpected error in configuration %+v", errs)
|
2017-08-02 05:51:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(c.ImageName, "packer-") {
|
|
|
|
t.Errorf("got default ImageName %q, want image name 'packer-{{timestamp}}'", c.ImageName)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-04-11 05:20:40 -04:00
|
|
|
t.Run("user_ocid_overridden", func(t *testing.T) {
|
|
|
|
expected := "override"
|
|
|
|
raw := testConfig(cfgFile)
|
|
|
|
raw["user_ocid"] = expected
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
var c Config
|
|
|
|
errs := c.Prepare(raw)
|
2018-04-11 05:20:40 -04:00
|
|
|
if errs != nil {
|
|
|
|
t.Fatalf("Unexpected error in configuration %+v", errs)
|
|
|
|
}
|
|
|
|
|
2019-10-14 10:08:16 -04:00
|
|
|
user, _ := c.configProvider.UserOCID()
|
2018-04-11 05:20:40 -04:00
|
|
|
if user != expected {
|
|
|
|
t.Errorf("Expected ConfigProvider.UserOCID: %s, got %s", expected, user)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("tenancy_ocid_overidden", func(t *testing.T) {
|
|
|
|
expected := "override"
|
|
|
|
raw := testConfig(cfgFile)
|
|
|
|
raw["tenancy_ocid"] = expected
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
var c Config
|
|
|
|
errs := c.Prepare(raw)
|
2018-04-11 05:20:40 -04:00
|
|
|
if errs != nil {
|
|
|
|
t.Fatalf("Unexpected error in configuration %+v", errs)
|
|
|
|
}
|
|
|
|
|
2019-10-14 10:08:16 -04:00
|
|
|
tenancy, _ := c.configProvider.TenancyOCID()
|
2018-04-11 05:20:40 -04:00
|
|
|
if tenancy != expected {
|
|
|
|
t.Errorf("Expected ConfigProvider.TenancyOCID: %s, got %s", expected, tenancy)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("region_overidden", func(t *testing.T) {
|
|
|
|
expected := "override"
|
|
|
|
raw := testConfig(cfgFile)
|
|
|
|
raw["region"] = expected
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
var c Config
|
|
|
|
errs := c.Prepare(raw)
|
2018-04-11 05:20:40 -04:00
|
|
|
if errs != nil {
|
|
|
|
t.Fatalf("Unexpected error in configuration %+v", errs)
|
|
|
|
}
|
|
|
|
|
2019-10-14 10:08:16 -04:00
|
|
|
region, _ := c.configProvider.Region()
|
2018-04-11 05:20:40 -04:00
|
|
|
if region != expected {
|
|
|
|
t.Errorf("Expected ConfigProvider.Region: %s, got %s", expected, region)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("fingerprint_overidden", func(t *testing.T) {
|
|
|
|
expected := "override"
|
|
|
|
raw := testConfig(cfgFile)
|
|
|
|
raw["fingerprint"] = expected
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
var c Config
|
|
|
|
errs := c.Prepare(raw)
|
2018-04-11 05:20:40 -04:00
|
|
|
if errs != nil {
|
|
|
|
t.Fatalf("Unexpected error in configuration: %+v", errs)
|
|
|
|
}
|
|
|
|
|
2019-10-14 10:08:16 -04:00
|
|
|
fingerprint, _ := c.configProvider.KeyFingerprint()
|
2018-04-11 05:20:40 -04:00
|
|
|
if fingerprint != expected {
|
|
|
|
t.Errorf("Expected ConfigProvider.KeyFingerprint: %s, got %s", expected, fingerprint)
|
|
|
|
}
|
|
|
|
})
|
2020-03-15 13:22:06 -04:00
|
|
|
|
|
|
|
// Test the correct errors are produced when certain template keys
|
|
|
|
// are present alongside use_instance_principals key.
|
|
|
|
invalidKeys := []string{
|
|
|
|
"access_cfg_file",
|
|
|
|
"access_cfg_file_account",
|
|
|
|
"user_ocid",
|
|
|
|
"tenancy_ocid",
|
|
|
|
"region",
|
|
|
|
"fingerprint",
|
|
|
|
"key_file",
|
|
|
|
"pass_phrase",
|
|
|
|
}
|
|
|
|
for _, k := range invalidKeys {
|
|
|
|
t.Run(k+"_mixed_with_use_instance_principals", func(t *testing.T) {
|
|
|
|
raw := testConfig(cfgFile)
|
|
|
|
raw["use_instance_principals"] = "true"
|
|
|
|
raw[k] = "some_random_value"
|
|
|
|
|
|
|
|
var c Config
|
|
|
|
|
|
|
|
c.configProvider = instancePrincipalConfigurationProviderMock{}
|
|
|
|
|
|
|
|
errs := c.Prepare(raw)
|
|
|
|
|
|
|
|
if !strings.Contains(errs.Error(), k) {
|
|
|
|
t.Errorf("Expected '%s' to contain '%s'", errs.Error(), k)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-04-11 05:20:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// BaseTestConfig creates the base (DEFAULT) config including a temporary key
|
|
|
|
// file.
|
|
|
|
// NOTE: Caller is responsible for removing temporary key file.
|
|
|
|
func baseTestConfigWithTmpKeyFile() (*ini.File, *os.File, error) {
|
|
|
|
keyFile, err := generateRSAKeyFile()
|
|
|
|
if err != nil {
|
|
|
|
return nil, keyFile, err
|
2017-02-13 05:35:14 -05:00
|
|
|
}
|
2018-04-11 05:20:40 -04:00
|
|
|
// Build ini
|
|
|
|
cfg := ini.Empty()
|
|
|
|
section, _ := cfg.NewSection("DEFAULT")
|
|
|
|
section.NewKey("region", "us-ashburn-1")
|
|
|
|
section.NewKey("tenancy", "ocid1.tenancy.oc1..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
|
|
|
|
section.NewKey("user", "ocid1.user.oc1..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
|
|
|
|
section.NewKey("fingerprint", "70:04:5z:b3:19:ab:90:75:a4:1f:50:d4:c7:c3:33:20")
|
|
|
|
section.NewKey("key_file", keyFile.Name())
|
|
|
|
|
|
|
|
return cfg, keyFile, nil
|
|
|
|
}
|
2017-02-13 05:35:14 -05:00
|
|
|
|
2018-04-11 05:20:40 -04:00
|
|
|
// WriteTestConfig writes a ini.File to a temporary file for use in unit tests.
|
|
|
|
// NOTE: Caller is responsible for removing temporary file.
|
|
|
|
func writeTestConfig(cfg *ini.File) (*os.File, error) {
|
|
|
|
confFile, err := ioutil.TempFile("", "config_file")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-13 05:35:14 -05:00
|
|
|
|
2018-04-11 05:20:40 -04:00
|
|
|
if _, err := confFile.Write([]byte("[DEFAULT]\n")); err != nil {
|
|
|
|
os.Remove(confFile.Name())
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-13 05:35:14 -05:00
|
|
|
|
2018-04-11 05:20:40 -04:00
|
|
|
if _, err := cfg.WriteTo(confFile); err != nil {
|
|
|
|
os.Remove(confFile.Name())
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return confFile, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// generateRSAKeyFile generates an RSA key file for use in unit tests.
|
|
|
|
// NOTE: The caller is responsible for deleting the temporary file.
|
|
|
|
func generateRSAKeyFile() (*os.File, error) {
|
|
|
|
// Create temporary file for the key
|
|
|
|
f, err := ioutil.TempFile("", "key")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-02-13 05:35:14 -05:00
|
|
|
}
|
2018-04-11 05:20:40 -04:00
|
|
|
|
|
|
|
// Generate key
|
|
|
|
priv, err := rsa.GenerateKey(rand.Reader, 2014)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ASN.1 DER encoded form
|
|
|
|
privDer := x509.MarshalPKCS1PrivateKey(priv)
|
|
|
|
privBlk := pem.Block{
|
|
|
|
Type: "RSA PRIVATE KEY",
|
|
|
|
Headers: nil,
|
|
|
|
Bytes: privDer,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the key out
|
|
|
|
if _, err := f.Write(pem.EncodeToMemory(&privBlk)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return f, nil
|
2017-02-13 05:35:14 -05:00
|
|
|
}
|