Commit 948dbfea authored by Pierre Smeyers's avatar Pierre Smeyers
Browse files

Merge branch 'fix/workaround-gitlab-default' into 'master'

Workaround GitLab behavior when a variable with forced service container visibility is unset

See merge request to-be-continuous/tools/vault-secrets-provider!109
parents 6bc49f75 7fa408d0
Loading
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
@@ -32,6 +32,5 @@

### VS Code ###
/.vscode/
reports/coverage.out
reports/coverage.out
reports/
start_vault.sh
+17 −37
Original line number Diff line number Diff line
# included templates
include:
  # $TBC_NAMESPACE is a group variable; can be globally overridden
  # Go template
  - project: "$TBC_NAMESPACE/golang"
    ref: "4.8"
    file: "templates/gitlab-ci-golang.yml"
  # Docker template
  - project: "$TBC_NAMESPACE/docker"
    ref: "5.9"
    file: "templates/gitlab-ci-docker.yml"
  # semantic-release template
  - project: "$TBC_NAMESPACE/semantic-release"
    ref: "3.8"
    file: "templates/gitlab-ci-semrel.yml"
  - project: '$TBC_NAMESPACE/gitleaks'
    ref: '2.5'
    file: '/templates/gitlab-ci-gitleaks.yml'  

# your pipeline stages
stages:
  - build
  - test
  - package-build
  - package-test
  - infra
  - deploy
  - acceptance
  - publish
  - infra-prod
  - production

# Global variables
variables:
  GO_BUILD_FLAGS: -tags netgo
  - component: "$CI_SERVER_FQDN/$TBC_NAMESPACE/docker/gitlab-ci-docker@6"
    inputs:
      build-args: "--build-arg CI_PROJECT_URL --build-arg DEFAULT_VAULT_URL"
      prod-publish-strategy: "auto"
      release-extra-tags: "latest \\g<major>.\\g<minor>\\g<build> \\g<major>\\g<build>"
  # Go template
  - component: "$CI_SERVER_FQDN/$TBC_NAMESPACE/golang/gitlab-ci-golang@4"
    inputs:
      build-flags: -tags netgo
      # force application build
  GO_BUILD_MODE: application
  GO_SBOM_OPTS: "-main cmd/vault_service"
  DOCKER_BUILD_ARGS: "--build-arg CI_PROJECT_URL --build-arg DEFAULT_VAULT_URL"
  DOCKER_PROD_PUBLISH_STRATEGY: "auto"
  DOCKER_RELEASE_EXTRA_TAGS: "latest \\g<major>.\\g<minor>\\g<build> \\g<major>\\g<build>"
  SEMREL_AUTO_RELEASE_ENABLED: "true"
      build-mode: application
      sbom-opts: "-main cmd/vault_service"
  # semantic-release template
  - component: "$CI_SERVER_FQDN/$TBC_NAMESPACE/semantic-release/gitlab-ci-semrel@3"
    inputs:
      auto-release-enabled: true
  # Gitleaks template
  - component: "$CI_SERVER_FQDN/$TBC_NAMESPACE/gitleaks/gitlab-ci-gitleaks@2"

# this job tests the Docker image on a remote Vault server (configurable)
test-on-remote:
+6 −0
Original line number Diff line number Diff line
@@ -28,6 +28,12 @@ type EnvStr string

func (env EnvStr) Or(def string) string {
	if v, ok := os.LookupEnv(string(env)); ok {
		if v == "$"+string(env) || v == "" {
			// workaround of GitLab behavior when defining a variable as:
			// VAR: "$VAR"
			// when VAR is unset at project level, its value remains literally '$VAR'
			return def
		}
		return v
	}

+56 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 Orange & contributors
 *
 * This program is free software; you can redistribute it and/or modify it under the terms
 *
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
 * Floor, Boston, MA  02110-1301, USA.
 */

package internal

import (
	"os"
	"testing"
)

func Test_getenv_when_var_unset(t *testing.T) {
	os.Unsetenv("TESTVAR")
	// os.Setenv("TESTVAR", "test")
	val := EnvStr("TESTVAR").Or("default")
	if val != "default" {
		t.Fatalf("Assert error\nExpected:\n%s\nGot:\n%s", "default", val)
	}
}

func Test_getenv_when_var_empty(t *testing.T) {
	os.Setenv("TESTVAR", "")
	val := EnvStr("TESTVAR").Or("default")
	if val != "default" {
		t.Fatalf("Assert error\nExpected:\n%s\nGot:\n%s", "default", val)
	}
}

func Test_getenv_when_var_gitlab_unset(t *testing.T) {
	os.Setenv("TESTVAR", "$TESTVAR")
	val := EnvStr("TESTVAR").Or("default")
	if val != "default" {
		t.Fatalf("Assert error\nExpected:\n%s\nGot:\n%s", "default", val)
	}
}

func Test_getenv_when_var_set(t *testing.T) {
	os.Setenv("TESTVAR", "testval")
	val := EnvStr("TESTVAR").Or("default")
	if val != "testval" {
		t.Fatalf("Assert error\nExpected:\n%s\nGot:\n%s", "testval", val)
	}
}