Skip to content

Commit

Permalink
Log error if any during base64 decoding (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
QubitPi authored Oct 1, 2024
1 parent 9f47657 commit 7d4530a
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 41 deletions.
16 changes: 6 additions & 10 deletions .github/workflows/ci-cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,23 +111,19 @@ jobs:
- name: Install plugin
run: packer plugins install --path packer-plugin-hashistack github.com/QubitPi/hashistack
- name: Run all acceptance tests
continue-on-error: true
run: PACKER_ACC=1 go test -count 1 -v ./... -timeout=120m
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
- name: Upload test logs
uses: actions/upload-artifact@v4
with:
name: "acc-test-log"
path: provisioner/**/packer_log_**.txt

release:
needs: [acceptance-tests]
if: ${{ github.ref == 'refs/heads/master' && github.repository != 'QubitPi/hashicorp-packer-plugin-scaffolding' }}
uses: ./.github/workflows/release.yml
secrets: inherit

slack-notification:
name: Send Slack Notification
if: github.ref != 'refs/heads/master'
needs: acceptance-tests
uses: QubitPi/hashistack/.github/workflows/slack-notification.yml@master
with:
job-status: ${{ needs.acceptance-tests.outputs.outcome }}
secrets:
slack-webhook-url: ${{ secrets.SLACK_WEBHOOK_URL }}
6 changes: 3 additions & 3 deletions .web-docs/components/provisioner/kong-api-gateway/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ build {
[HashiCorp Terraform - Install]: https://terraform.qubitpi.org/terraform/install
[HashiCorp Terraform variable values file]: https://terraform.qubitpi.org/terraform/language/values/variables#variable-definitions-tfvars-files

[Kong API Gateway]: https://qubitpi.github.io/docs.konghq.com/gateway/latest/
[Kong manager UI]: https://qubitpi.github.io/docs.konghq.com/gateway/latest/kong-manager/
[Kong gateway - various ports]: https://qubitpi.github.io/docs.konghq.com/gateway/latest/production/networking/default-ports/
[Kong API Gateway]: https://kong.qubitpi.org/gateway/latest/
[Kong manager UI]: https://kong.qubitpi.org/gateway/latest/kong-manager/
[Kong gateway - various ports]: https://kong.qubitpi.org/gateway/latest/production/networking/default-ports/

[Let's Encrypt]: https://qubitpi.github.io/letsencrypt-website/
6 changes: 3 additions & 3 deletions docs/provisioners/kong-api-gateway.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ build {
[HashiCorp Terraform - Install]: https://terraform.qubitpi.org/terraform/install
[HashiCorp Terraform variable values file]: https://terraform.qubitpi.org/terraform/language/values/variables#variable-definitions-tfvars-files

[Kong API Gateway]: https://qubitpi.github.io/docs.konghq.com/gateway/latest/
[Kong manager UI]: https://qubitpi.github.io/docs.konghq.com/gateway/latest/kong-manager/
[Kong gateway - various ports]: https://qubitpi.github.io/docs.konghq.com/gateway/latest/production/networking/default-ports/
[Kong API Gateway]: https://kong.qubitpi.org/gateway/latest/
[Kong manager UI]: https://kong.qubitpi.org/gateway/latest/kong-manager/
[Kong gateway - various ports]: https://kong.qubitpi.org/gateway/latest/production/networking/default-ports/

[Let's Encrypt]: https://qubitpi.github.io/letsencrypt-website/
9 changes: 6 additions & 3 deletions provisioner/docker-mailserver/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,26 @@ func (p *Provisioner) Provision(ctx context.Context, ui packersdk.Ui, communicat
composeFileSource, err := ssl.WriteToFile(composeFile)
err = file.Provision(p.config.ctx, ui, communicator, composeFileSource, composeFileDst)
if err != nil {
return fmt.Errorf("error uploading '%s' to '%s': %s", composeFileSource, composeFileDst, err)
ui.Say(fmt.Sprintf("error uploading '%s' to '%s': %s", composeFileSource, composeFileDst, err))
panic(err)
}

sslCert, err := ssl.DecodeBase64(p.config.SslCertBase64)
sslCertSource, err := ssl.WriteToFile(sslCert)
sslCertDestination := fmt.Sprintf(filepath.Join(p.config.HomeDir, "fullchain.pem"))
err = file.Provision(p.config.ctx, ui, communicator, sslCertSource, sslCertDestination)
if err != nil {
return fmt.Errorf("error uploading '%s' to '%s': %s", sslCertSource, sslCertDestination, err)
ui.Say(fmt.Sprintf("error uploading '%s' to '%s': %s", sslCertSource, sslCertDestination, err))
panic(err)
}

sslCertKey, err := ssl.DecodeBase64(p.config.SslCertKeyBase64)
sslCertKeySource, err := ssl.WriteToFile(sslCertKey)
sslCertKeyDestination := fmt.Sprintf(filepath.Join(p.config.HomeDir, "privkey.pem"))
err = file.Provision(p.config.ctx, ui, communicator, sslCertKeySource, sslCertKeyDestination)
if err != nil {
return fmt.Errorf("error uploading '%s' to '%s': %s", sslCertKeySource, sslCertKeyDestination, err)
ui.Say(fmt.Sprintf("error uploading '%s' to '%s': %s", sslCertKeySource, sslCertKeyDestination, err))
panic(err)
}

return shell.Provision(ctx, ui, communicator, getCommands(p.config.HomeDir, mailServerDomain, sslCertDestination, sslCertKeyDestination))
Expand Down
6 changes: 4 additions & 2 deletions provisioner/file-provisioner/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ import (
func Provision(ctx interpolate.Context, ui packersdk.Ui, communicator packersdk.Communicator, source string, destination string) error {
src, err := interpolate.Render(source, &ctx)
if err != nil {
return fmt.Errorf("error interpolating source: %s", err)
ui.Say(fmt.Sprintf("error interpolating source: %s", err))
panic(err)
}

dst, err := interpolate.Render(destination, &ctx)
if err != nil {
return fmt.Errorf("error interpolating destination: %s", err)
ui.Say(fmt.Sprintf("error interpolating destination: %s", err))
panic(err)
}

ui.Say(fmt.Sprintf("Uploading %s => %s", src, dst))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ build {

provisioner "hashistack-kong-api-gateway-provisioner" {
homeDir = "/home/ubuntu"
sslCertBase64 = "YXNkZnNnaHRkeWhyZXJ3ZGZydGV3ZHNmZ3RoeTY0cmV3ZGZyZWd0cmV3d2ZyZw=="
sslCertKeyBase64 = "MzI0NXRnZjk4dmJoIGNsO2VbNDM1MHRdzszNDM1b2l0cmo="
sslCertBase64 = "VGhpcyBpcyBhIHRlc3QgY2VydA=="
sslCertKeyBase64 = "VGhpcyBpcyBhIHRlc3QgY2VydA=="
kongApiGatewayDomain = "api.mycompany.com"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ build {

provisioner "hashistack-kong-api-gateway-provisioner" {
homeDir = "/"
sslCertBase64 = "YXNkZnNnaHRkeWhyZXJ3ZGZydGV3ZHNmZ3RoeTY0cmV3ZGZyZWd0cmV3d2ZyZw=="
sslCertKeyBase64 = "MzI0NXRnZjk4dmJoIGNsO2VbNDM1MHRdzszNDM1b2l0cmo="
sslCertBase64 = "VGhpcyBpcyBhIHRlc3QgY2VydA=="
sslCertKeyBase64 = "VGhpcyBpcyBhIHRlc3QgY2VydA=="
kongApiGatewayDomain = "api.mycompany.com"
}
}
4 changes: 2 additions & 2 deletions provisioner/react/test-fixtures/template-aws.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ build {
provisioner "hashistack-react-provisioner" {
distSource = "/my/path/to/dist"
homeDir = "/home/ubuntu"
sslCertBase64 = "YXNkZnNnaHRkeWhyZXJ3ZGZydGV3ZHNmZ3RoeTY0cmV3ZGZyZWd0cmV3d2ZyZw=="
sslCertKeyBase64 = "MzI0NXRnZjk4dmJoIGNsO2VbNDM1MHRdzszNDM1b2l0cmo="
sslCertBase64 = "VGhpcyBpcyBhIHRlc3QgY2VydA=="
sslCertKeyBase64 = "VGhpcyBpcyBhIHRlc3QgY2VydA=="
appDomain = "app.mycompany.com"
}
}
4 changes: 2 additions & 2 deletions provisioner/react/test-fixtures/template-docker.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ build {
provisioner "hashistack-react-provisioner" {
distSource = "/my/path/to/dist"
homeDir = "/"
sslCertBase64 = "YXNkZnNnaHRkeWhyZXJ3ZGZydGV3ZHNmZ3RoeTY0cmV3ZGZyZWd0cmV3d2ZyZw=="
sslCertKeyBase64 = "MzI0NXRnZjk4dmJoIGNsO2VbNDM1MHRdzszNDM1b2l0cmo="
sslCertBase64 = "VGhpcyBpcyBhIHRlc3QgY2VydA=="
sslCertKeyBase64 = "VGhpcyBpcyBhIHRlc3QgY2VydA=="
appDomain = "app.mycompany.com"
}
}
12 changes: 8 additions & 4 deletions provisioner/shell/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ import (
func Provision(ctx context.Context, ui packersdk.Ui, communicator packersdk.Communicator, commands []string) error {
scriptFile, err := loadCommandsIntoScript(commands)
if err != nil {
return err
ui.Say(fmt.Sprintf("Error loading commands into script: %s", err))
panic(err)
}
defer os.Remove(scriptFile.Name())

Expand Down Expand Up @@ -95,7 +96,8 @@ func loadCommandsIntoScript(commands []string) (*os.File, error) {
func executeScript(ctx context.Context, ui packersdk.Ui, communicator packersdk.Communicator, scriptFile *os.File) error {
f, err := os.Open(scriptFile.Name())
if err != nil {
return fmt.Errorf("error opening shell script: %s", err)
ui.Say(fmt.Sprintf("error opening shell script: %s", err))
panic(err)
}
defer f.Close()

Expand All @@ -107,14 +109,16 @@ func executeScript(ctx context.Context, ui packersdk.Ui, communicator packersdk.

remotePath := fmt.Sprintf("%s/%s", "/tmp", fmt.Sprintf("script_%d.sh", rand.Intn(9999)))
if err := communicator.Upload(remotePath, f, nil); err != nil {
return fmt.Errorf("error uploading script: %s", err)
ui.Say(fmt.Sprintf("error uploading script: %s", err))
panic(err)
}

cmd = &packersdk.RemoteCmd{
Command: fmt.Sprintf("chmod 0755 %s", remotePath),
}
if err := communicator.Start(ctx, cmd); err != nil {
return fmt.Errorf("error chmodding script file to 0755 in remote machine: %s", err)
ui.Say(fmt.Sprintf("error chmodding script file to 0755 in remote machine: %s", err))
panic(err)
}
cmd.Wait()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ build {

provisioner "hashistack-sonatype-nexus-repository-provisioner" {
homeDir = "/home/ubuntu"
sslCertBase64 = "YXNkZnNnaHRkeWhyZXJ3ZGZydGV3ZHNmZ3RoeTY0cmV3ZGZyZWd0cmV3d2ZyZw=="
sslCertKeyBase64 = "MzI0NXRnZjk4dmJoIGNsO2VbNDM1MHRdzszNDM1b2l0cmo="
sslCertBase64 = "VGhpcyBpcyBhIHRlc3QgY2VydA=="
sslCertKeyBase64 = "VGhpcyBpcyBhIHRlc3QgY2VydA=="
sonatypeNexusRepositoryDomain = "nexus.mycompany.com"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ build {

provisioner "hashistack-sonatype-nexus-repository-provisioner" {
homeDir = "/"
sslCertBase64 = "YXNkZnNnaHRkeWhyZXJ3ZGZydGV3ZHNmZ3RoeTY0cmV3ZGZyZWd0cmV3d2ZyZw=="
sslCertKeyBase64 = "MzI0NXRnZjk4dmJoIGNsO2VbNDM1MHRdzszNDM1b2l0cmo="
sslCertBase64 = "VGhpcyBpcyBhIHRlc3QgY2VydA=="
sslCertKeyBase64 = "VGhpcyBpcyBhIHRlc3QgY2VydA=="
sonatypeNexusRepositoryDomain = "nexus.mycompany.com"
}
}
20 changes: 16 additions & 4 deletions provisioner/ssl-provisioner/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,39 @@ func Provision(
nginxConfig string,
) error {
sslCert, err := DecodeBase64(sslCertBase64)
if err != nil {
ui.Say(fmt.Sprintf("Error decoding SSL cert base64: %s", err))
panic(err)
}
sslCertSource, err := WriteToFile(sslCert)
sslCertDestination := fmt.Sprintf(filepath.Join(homeDir, sslCertFilename))
err = file.Provision(interCtx, ui, communicator, sslCertSource, sslCertDestination)
if err != nil {
return fmt.Errorf("error uploading '%s' to '%s': %s", sslCertSource, sslCertDestination, err)
ui.Say(fmt.Sprintf("error uploading '%s' to '%s': %s", sslCertSource, sslCertDestination, err))
panic(err)
}

sslCertKey, err := DecodeBase64(sslCertKeyBase64)
if err != nil {
ui.Say(fmt.Sprintf("Error decoding SSL cert key base64: %s", err))
panic(err)
}
sslCertKeySource, err := WriteToFile(sslCertKey)
sslCertKeyDestination := fmt.Sprintf(filepath.Join(homeDir, sslCertKeyFilename))
err = file.Provision(interCtx, ui, communicator, sslCertKeySource, sslCertKeyDestination)
if err != nil {
return fmt.Errorf("error uploading '%s' to '%s': %s", sslCertKeySource, sslCertKeyDestination, err)
ui.Say(fmt.Sprintf("error uploading '%s' to '%s': %s", sslCertKeySource, sslCertKeyDestination, err))
panic(err)
}

if nginxConfig != "" {
nginxSource, err := WriteToFile(nginxConfig)
nginxDst := fmt.Sprintf(filepath.Join(homeDir, nginxConfigFilename))
err = file.Provision(interCtx, ui, communicator, nginxSource, nginxDst)

if err != nil {
return fmt.Errorf("error uploading '%s' to '%s': %s", nginxSource, nginxDst, err)
ui.Say(fmt.Sprintf("error uploading '%s' to '%s': %s", nginxSource, nginxDst, err))
panic(err)
}
}

Expand Down Expand Up @@ -100,7 +112,7 @@ func WriteToFile(content string) (string, error) {
func DecodeBase64(encoded string) (string, error) {
data, err := base64.StdEncoding.DecodeString(encoded)
if err != nil {
return "", fmt.Errorf("error interpolating destination: %s", err)
return "", fmt.Errorf("error decoding base64 string: %s", err)
}
return string(data), nil
}
Expand Down

0 comments on commit 7d4530a

Please sign in to comment.