Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/azure cert deploy #1132

Merged
merged 12 commits into from
Jul 3, 2024
2 changes: 1 addition & 1 deletion atrium/vestibulum/trcdb/trcapimgmtbase/trcapimgmtbase.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func CommonMain(envPtr *string,
return err
}

//Adding a 3 minute timeout on APIM Update.
//Adding a 2 minute timeout on APIM Update.
go func(ctxC context.CancelFunc) {
time.Sleep(time.Second * 120)
ctxC()
Expand Down
86 changes: 86 additions & 0 deletions atrium/vestibulum/trcdb/trccertmgmtbase/trccertmgmtbase.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package trccertmgmtbase

import (
"context"
"encoding/base64"
"errors"
"fmt"
"os"
"strings"
"time"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/apimanagement/armapimanagement/v2"
eUtils "github.com/trimble-oss/tierceron/pkg/utils"
"github.com/trimble-oss/tierceron/pkg/vaulthelper/kv"
)

func CommonMain(certPathPtr *string, driverConfig *eUtils.DriverConfig, mod *kv.Modifier) error {
if len(*certPathPtr) == 0 {
return errors.New("certPath flag is empty, expected path to cert")
}

certBytes, err := os.ReadFile(*certPathPtr)
if err != nil {
return err
}

apimConfigMap := make(map[string]string)
tempMap, readErr := mod.ReadData("super-secrets/Restricted/APIMConfig/config")
if readErr != nil {
return readErr
} else if len(tempMap) == 0 {
return errors.New("Couldn't get apim configs for update.")
}

for key, value := range tempMap {
apimConfigMap[fmt.Sprintf("%v", key)] = fmt.Sprintf("%v", value)
}

svc, err := azidentity.NewClientSecretCredential(
apimConfigMap["azureTenantId"],
apimConfigMap["azureClientId"],
apimConfigMap["azureClientSecret"],
nil)
if err != nil {
driverConfig.CoreConfig.Log.Fatalf("failed to obtain a credential: %v", err)
return err
}

ctx, _ := context.WithCancel(context.Background())
clientFactory, err := armapimanagement.NewClientFactory(apimConfigMap["SUBSCRIPTION_ID"], svc, nil)
if err != nil {
driverConfig.CoreConfig.Log.Fatalf("failed to create client: %v", err)
return err
}

resourceGroupName, exists := apimConfigMap["RESOURCE_GROUP_NAME"]
isaac-boaz-trimble marked this conversation as resolved.
Show resolved Hide resolved
if !exists {
return errors.New("RESOURCE_GROUP_NAME is not populated in apimConfigMap")
}

serviceName, exists := apimConfigMap["SERVICE_NAME"]
isaac-boaz-trimble marked this conversation as resolved.
Show resolved Hide resolved
if !exists {
return errors.New("SERVICE_NAME is not populated in apimConfigMap")
}

certificateId := time.Now().UTC().Format(strings.ReplaceAll(time.RFC3339, ":", "-"))

etag := "*"

_, err = clientFactory.NewCertificateClient().CreateOrUpdate(ctx, resourceGroupName, serviceName, certificateId, armapimanagement.CertificateCreateOrUpdateParameters{
Properties: &armapimanagement.CertificateCreateOrUpdateProperties{
Data: to.Ptr(base64.StdEncoding.EncodeToString(certBytes)),
Password: to.Ptr(apimConfigMap["CERTIFICATE_PASSWORD"]),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check for existence as well.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If not in tierceron template somewhere, it should be.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the cert doesn't have an associated password, would we want to pass nil as a pointer, or do a different function call omitting the password field to the properties?

},
}, &armapimanagement.CertificateClientCreateOrUpdateOptions{IfMatch: &etag})

if err != nil {
driverConfig.CoreConfig.Log.Fatalf("failed to finish the request: %v", err)
return err
}

fmt.Printf("Certificate %v successfully deployed\n", certificateId)
return nil
}
13 changes: 13 additions & 0 deletions atrium/vestibulum/trcdb/trcplgtoolbase/trcplgtoolbase.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
eUtils "github.com/trimble-oss/tierceron/pkg/utils"

trcapimgmtbase "github.com/trimble-oss/tierceron/atrium/vestibulum/trcdb/trcapimgmtbase"
"github.com/trimble-oss/tierceron/atrium/vestibulum/trcdb/trccertmgmtbase"
)

func CommonMain(envDefaultPtr *string,
Expand Down Expand Up @@ -95,6 +96,9 @@ func CommonMain(envDefaultPtr *string,
//APIM flags
updateAPIMPtr := flagset.Bool("updateAPIM", false, "Used to update Azure APIM")

// Cert flags
certPathPtr := flagset.String("certPath", "", "Path to certificate to push to Azure")

if trcshDriverConfig == nil || !trcshDriverConfig.DriverConfig.IsShellSubProcess {
args := argLines[1:]
for i := 0; i < len(args); i++ {
Expand Down Expand Up @@ -340,6 +344,15 @@ func CommonMain(envDefaultPtr *string,
return nil
}

if len(*certPathPtr) > 0 {
updateCertError := trccertmgmtbase.CommonMain(certPathPtr, config, mod)
if updateCertError != nil {
fmt.Println(updateCertError.Error())
isaac-boaz-trimble marked this conversation as resolved.
Show resolved Hide resolved
fmt.Println("Couldn't update Cert...proceeding with build")
}
return nil
}

// Get existing configs if they exist...
pluginToolConfig, plcErr := trcvutils.GetPluginToolConfig(&trcshDriverConfigBase.DriverConfig, mod, coreopts.BuildOptions.ProcessDeployPluginEnvConfig(map[string]interface{}{}), *defineServicePtr)
if plcErr != nil {
Expand Down