2018-12-03 22:19:18 +03:00
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
// Licensed under the MIT license.
|
|
|
|
|
2018-12-03 23:01:25 +03:00
|
|
|
package engine
|
2018-07-05 20:58:23 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"testing"
|
|
|
|
|
2019-02-15 20:21:25 +03:00
|
|
|
"github.com/Azure/go-autorest/autorest/azure"
|
|
|
|
|
2018-12-03 23:01:25 +03:00
|
|
|
"github.com/Azure/aks-engine/pkg/api"
|
|
|
|
"github.com/Azure/aks-engine/pkg/helpers"
|
|
|
|
"github.com/Azure/aks-engine/pkg/i18n"
|
2018-07-05 20:58:23 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestWriteTLSArtifacts(t *testing.T) {
|
|
|
|
|
2021-02-11 21:30:09 +03:00
|
|
|
cs := api.CreateMockContainerService("testcluster", "", 1, 2, true)
|
2018-07-05 20:58:23 +03:00
|
|
|
writer := &ArtifactWriter{
|
|
|
|
Translator: &i18n.Translator{
|
|
|
|
Locale: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
dir := "_testoutputdir"
|
2018-09-25 03:14:43 +03:00
|
|
|
defaultDir := fmt.Sprintf("%s-%s", cs.Properties.OrchestratorProfile.OrchestratorType, cs.Properties.GetClusterID())
|
2018-07-05 20:58:23 +03:00
|
|
|
defaultDir = path.Join("_output", defaultDir)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer os.RemoveAll(defaultDir)
|
|
|
|
|
|
|
|
// Generate apimodel and azure deploy artifacts without certs
|
|
|
|
err := writer.WriteTLSArtifacts(cs, "vlabs", "fake template", "fake parameters", dir, false, false)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error trying to write TLS artifacts: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedFiles := []string{"apimodel.json", "azuredeploy.json", "azuredeploy.parameters.json"}
|
|
|
|
|
|
|
|
for _, f := range expectedFiles {
|
2019-03-12 01:55:50 +03:00
|
|
|
if _, err = os.Stat(dir + "/" + f); os.IsNotExist(err) {
|
2018-07-05 20:58:23 +03:00
|
|
|
t.Fatalf("expected file %s/%s to be generated by WriteTLSArtifacts", dir, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
os.RemoveAll(dir)
|
|
|
|
|
|
|
|
// Generate parameters only and certs
|
|
|
|
err = writer.WriteTLSArtifacts(cs, "vlabs", "fake template", "fake parameters", "", true, true)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error trying to write TLS artifacts: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2019-03-12 01:55:50 +03:00
|
|
|
if _, err = os.Stat(defaultDir + "/apimodel.json"); !os.IsNotExist(err) {
|
2018-07-05 20:58:23 +03:00
|
|
|
t.Fatalf("expected file %s/apimodel.json not to be generated by WriteTLSArtifacts with parametersOnly set to true", defaultDir)
|
|
|
|
}
|
|
|
|
|
2019-03-12 01:55:50 +03:00
|
|
|
if _, err = os.Stat(defaultDir + "/azuredeploy.json"); !os.IsNotExist(err) {
|
2018-07-05 20:58:23 +03:00
|
|
|
t.Fatalf("expected file %s/azuredeploy.json not to be generated by WriteTLSArtifacts with parametersOnly set to true", defaultDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedFiles = []string{"azuredeploy.parameters.json", "ca.crt", "ca.key", "apiserver.crt", "apiserver.key", "client.crt", "client.key", "etcdclient.key", "etcdclient.crt", "etcdserver.crt", "etcdserver.key", "etcdpeer0.crt", "etcdpeer0.key", "kubectlClient.crt", "kubectlClient.key"}
|
|
|
|
|
|
|
|
for _, f := range expectedFiles {
|
2019-03-12 01:55:50 +03:00
|
|
|
if _, err = os.Stat(defaultDir + "/" + f); os.IsNotExist(err) {
|
2018-07-05 20:58:23 +03:00
|
|
|
t.Fatalf("expected file %s/%s to be generated by WriteTLSArtifacts", dir, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
kubeDir := path.Join(defaultDir, "kubeconfig")
|
2019-03-12 01:55:50 +03:00
|
|
|
if _, err = os.Stat(kubeDir + "/" + "kubeconfig.eastus.json"); os.IsNotExist(err) {
|
2018-07-05 20:58:23 +03:00
|
|
|
t.Fatalf("expected file %s/kubeconfig/kubeconfig.eastus.json to be generated by WriteTLSArtifacts", defaultDir)
|
|
|
|
}
|
|
|
|
os.RemoveAll(defaultDir)
|
|
|
|
|
2019-02-15 20:21:25 +03:00
|
|
|
// Generate files with custom cloud profile in configuration
|
2021-02-11 21:30:09 +03:00
|
|
|
csCustom := api.CreateMockContainerService("testcluster", "", 1, 2, false)
|
2019-02-15 20:21:25 +03:00
|
|
|
csCustom.Location = "customlocation"
|
|
|
|
csCustom.Properties.CustomCloudProfile = &api.CustomCloudProfile{
|
|
|
|
Environment: &azure.Environment{
|
|
|
|
Name: "azurestackcloud",
|
|
|
|
ManagementPortalURL: "managementPortalURL",
|
|
|
|
PublishSettingsURL: "publishSettingsURL",
|
|
|
|
ServiceManagementEndpoint: "serviceManagementEndpoint",
|
|
|
|
ResourceManagerEndpoint: "resourceManagerEndpoint",
|
|
|
|
ActiveDirectoryEndpoint: "activeDirectoryEndpoint",
|
|
|
|
GalleryEndpoint: "galleryEndpoint",
|
|
|
|
KeyVaultEndpoint: "keyVaultEndpoint",
|
|
|
|
GraphEndpoint: "graphEndpoint",
|
|
|
|
ServiceBusEndpoint: "serviceBusEndpoint",
|
|
|
|
BatchManagementEndpoint: "batchManagementEndpoint",
|
|
|
|
StorageEndpointSuffix: "storageEndpointSuffix",
|
|
|
|
SQLDatabaseDNSSuffix: "sqlDatabaseDNSSuffix",
|
|
|
|
TrafficManagerDNSSuffix: "trafficManagerDNSSuffix",
|
|
|
|
KeyVaultDNSSuffix: "keyVaultDNSSuffix",
|
|
|
|
ServiceBusEndpointSuffix: "serviceBusEndpointSuffix",
|
|
|
|
ServiceManagementVMDNSSuffix: "serviceManagementVMDNSSuffix",
|
|
|
|
ResourceManagerVMDNSSuffix: "resourceManagerVMDNSSuffix",
|
|
|
|
ContainerRegistryDNSSuffix: "containerRegistryDNSSuffix",
|
|
|
|
TokenAudience: "tokenAudience",
|
|
|
|
},
|
|
|
|
}
|
2020-05-04 19:12:16 +03:00
|
|
|
_, err = csCustom.SetPropertiesDefaults(api.PropertiesDefaultsParams{
|
2019-09-06 19:16:20 +03:00
|
|
|
IsScale: false,
|
|
|
|
IsUpgrade: false,
|
|
|
|
PkiKeySize: helpers.DefaultPkiKeySize,
|
|
|
|
})
|
2020-05-04 19:12:16 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error trying to set properties defaults: %s", err.Error())
|
|
|
|
}
|
2019-02-15 20:21:25 +03:00
|
|
|
err = writer.WriteTLSArtifacts(csCustom, "vlabs", "fake template", "fake parameters", "", true, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error trying to write TLS artifacts: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedFiles = []string{"apimodel.json", "azuredeploy.json", "azuredeploy.parameters.json", "ca.crt", "ca.key", "apiserver.crt", "apiserver.key", "client.crt", "client.key", "etcdclient.key", "etcdclient.crt", "etcdserver.crt", "etcdserver.key", "etcdpeer0.crt", "etcdpeer0.key", "kubectlClient.crt", "kubectlClient.key"}
|
|
|
|
|
|
|
|
for _, f := range expectedFiles {
|
2019-03-12 01:55:50 +03:00
|
|
|
if _, err = os.Stat(defaultDir + "/" + f); os.IsNotExist(err) {
|
2019-02-15 20:21:25 +03:00
|
|
|
t.Fatalf("expected file %s/%s to be generated by WriteTLSArtifacts", dir, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
kubeDirCustom := path.Join(defaultDir, "kubeconfig")
|
2019-03-12 01:55:50 +03:00
|
|
|
if _, err = os.Stat(kubeDirCustom + "/" + "kubeconfig." + csCustom.Location + ".json"); os.IsNotExist(err) {
|
2019-02-15 20:21:25 +03:00
|
|
|
t.Fatalf("expected file %s/kubeconfig/kubeconfig.%s.json to be generated by WriteTLSArtifacts", csCustom.Location, defaultDir)
|
|
|
|
}
|
|
|
|
os.RemoveAll(defaultDir)
|
|
|
|
|
2018-07-05 20:58:23 +03:00
|
|
|
// Generate certs with all kubeconfig locations
|
|
|
|
cs.Location = ""
|
|
|
|
err = writer.WriteTLSArtifacts(cs, "vlabs", "fake template", "fake parameters", "", true, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error trying to write TLS artifacts: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2018-10-11 03:43:14 +03:00
|
|
|
for _, region := range helpers.GetAzureLocations() {
|
2018-07-05 20:58:23 +03:00
|
|
|
if _, err := os.Stat(kubeDir + "/" + "kubeconfig." + region + ".json"); os.IsNotExist(err) {
|
|
|
|
t.Fatalf("expected kubeconfig for region %s to be generated by WriteTLSArtifacts", region)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|