Skip to content
Snippets Groups Projects
Select Git revision
  • 2452-exec-docker-in-worktree
  • setup-secure-jobs
  • golangci-lint
  • 6624-build_test-dup
  • master default protected
  • switch-to-var
  • 12-9-stable
  • add-godoc-executor
  • docs-add-specific-runner-version
  • autoscale-aws-fargate-docs
  • docs-vichak-certificate-note
  • 17058-follow-up-from-retry-kubernetes-commands-when-error-dialing-backend-eof-error-is-hit-add
  • 4696-add-support-for-raw-variables
  • run-any-step-concatenated
  • run-tests-on-different-runners-when-on-main-fork
  • change-how-helper-tools-are-installed
  • 2076-arm64-docker
  • rename-pkg
  • docs/add-trainee-backend-maintainer-issue-template
  • add_openshift_support
  • v12.9.0
  • v12.9.0-rc3
  • v12.9.0-rc2
  • v12.9.0-rc1
  • v12.8.0
  • v12.8.0-rc1
  • v12.7.1
  • v12.7.0
  • v12.7.0-rc1
  • v12.6.0
  • v12.6.0-rc1
  • v12.5.0
  • v12.5.0-rc1
  • v12.4.1
  • v12.4.0
  • v12.4.0-rc2
  • v12.4.0-rc1
  • v12.3.0
  • v12.3.0-rc2
  • v12.3.0-rc1
40 results

credentials_resolver.go

Forked from GitLab.org / gitlab-runner
8530 commits behind, 4 commits ahead of the upstream repository.
credentials_resolver.go 2.31 KiB
package gcs

import (
	"encoding/json"
	"fmt"
	"io/ioutil"

	"github.com/sirupsen/logrus"

	"gitlab.com/gitlab-org/gitlab-runner/common"
)

type credentialsResolver interface {
	Credentials() *common.CacheGCSCredentials
	Resolve() error
}

const TypeServiceAccount = "service_account"

type credentialsFile struct {
	Type        string `json:"type"`
	ClientEmail string `json:"client_email"`
	PrivateKey  string `json:"private_key"`
}

type defaultCredentialsResolver struct {
	config      *common.CacheGCSConfig
	credentials *common.CacheGCSCredentials
}

func (cr *defaultCredentialsResolver) Credentials() *common.CacheGCSCredentials {
	return cr.credentials
}

func (cr *defaultCredentialsResolver) Resolve() error {
	if cr.config.CredentialsFile != "" {
		return cr.readCredentialsFromFile()
	}

	return cr.readCredentialsFromConfig()
}

func (cr *defaultCredentialsResolver) readCredentialsFromFile() error {
	data, err := ioutil.ReadFile(cr.config.CredentialsFile)
	if err != nil {
		return fmt.Errorf("error while reading credentials file: %v", err)
	}

	var credentialsFileContent credentialsFile
	err = json.Unmarshal(data, &credentialsFileContent)
	if err != nil {
		return fmt.Errorf("error while parsing credentials file: %v", err)
	}

	if credentialsFileContent.Type != TypeServiceAccount {
		return fmt.Errorf("unsupported credentials file type: %s", credentialsFileContent.Type)
	}

	logrus.Debugln("Credentials loaded from file. Skipping direct settings from Runner configuration file")

	cr.credentials.AccessID = credentialsFileContent.ClientEmail
	cr.credentials.PrivateKey = credentialsFileContent.PrivateKey

	return nil
}

func (cr *defaultCredentialsResolver) readCredentialsFromConfig() error {
	if cr.config.AccessID == "" || cr.config.PrivateKey == "" {
		return fmt.Errorf("GCS config present, but credentials are not configured")
	}

	cr.credentials.AccessID = cr.config.AccessID
	cr.credentials.PrivateKey = cr.config.PrivateKey

	return nil
}

func newDefaultCredentialsResolver(config *common.CacheGCSConfig) (*defaultCredentialsResolver, error) {
	if config == nil {
		return nil, fmt.Errorf("config can't be nil")
	}

	credentials := &defaultCredentialsResolver{
		config:      config,
		credentials: &common.CacheGCSCredentials{},
	}

	return credentials, nil
}

var credentialsResolverInitializer = newDefaultCredentialsResolver