Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ builds:
- -X github.com/argoproj/argo-cd/v3/common.gitCommit={{ .FullCommit }}
- -X github.com/argoproj/argo-cd/v3/common.gitTreeState={{ .Env.GIT_TREE_STATE }}
- -X github.com/argoproj/argo-cd/v3/common.kubectlVersion={{ .Env.KUBECTL_VERSION }}
- '{{ if or (eq .Runtime.Goos "linux") (eq .Runtime.Goos "windows") }}-extldflags="-static"{{ end }}'
- -extldflags="-static"
goos:
- linux
- windows
Expand All @@ -42,15 +42,6 @@ builds:
goarch: ppc64le
- goos: windows
goarch: arm64
overrides:
- goos: darwin
goarch: amd64
env:
- CGO_ENABLED=1
- goos: darwin
goarch: arm64
env:
- CGO_ENABLED=1

archives:
- id: argocd-archive
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !darwin || (cgo && darwin)

package commands

import (
Expand Down
25 changes: 25 additions & 0 deletions cmd/argocd-k8s-auth/commands/azure_no_cgo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//go:build darwin && !cgo

// Package commands
// This file is used when the GOOS is darwin and CGO is not enabled.
// It provides a no-op implementation of newAzureCommand to allow goreleaser to build
// a darwin binary on a linux machine.
package commands

import (
"log"

"github.com/spf13/cobra"

"github.com/argoproj/argo-cd/v3/util/workloadidentity"
)

func newAzureCommand() *cobra.Command {
command := &cobra.Command{
Use: "azure",
Run: func(c *cobra.Command, _ []string) {
log.Fatalf(workloadidentity.CGOError)
},
}
return command
}
30 changes: 0 additions & 30 deletions util/workloadidentity/workloadidentity.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package workloadidentity

import (
"context"
"time"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
)

const (
Expand All @@ -22,34 +17,9 @@ type TokenProvider interface {
GetToken(scope string) (*Token, error)
}

type WorkloadIdentityTokenProvider struct {
tokenCredential azcore.TokenCredential
}

// Used to propagate initialization error if any
var initError error

func NewWorkloadIdentityTokenProvider() TokenProvider {
cred, err := azidentity.NewDefaultAzureCredential(&azidentity.DefaultAzureCredentialOptions{})
initError = err
return WorkloadIdentityTokenProvider{tokenCredential: cred}
}

func (c WorkloadIdentityTokenProvider) GetToken(scope string) (*Token, error) {
if initError != nil {
return nil, initError
}

token, err := c.tokenCredential.GetToken(context.Background(), policy.TokenRequestOptions{
Scopes: []string{scope},
})
if err != nil {
return nil, err
}

return &Token{AccessToken: token.Token, ExpiresOn: token.ExpiresOn}, nil
}

func CalculateCacheExpiryBasedOnTokenExpiry(tokenExpiry time.Time) time.Duration {
// Calculate the cache expiry as 5 minutes before the token expires
cacheExpiry := time.Until(tokenExpiry) - time.Minute*5
Expand Down
36 changes: 36 additions & 0 deletions util/workloadidentity/workloadidentity_cgo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//go:build !darwin || (cgo && darwin)

package workloadidentity

import (
"context"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
)

type WorkloadIdentityTokenProvider struct {
tokenCredential azcore.TokenCredential
}

func NewWorkloadIdentityTokenProvider() TokenProvider {
cred, err := azidentity.NewDefaultAzureCredential(&azidentity.DefaultAzureCredentialOptions{})
initError = err
return WorkloadIdentityTokenProvider{tokenCredential: cred}
}

func (c WorkloadIdentityTokenProvider) GetToken(scope string) (*Token, error) {
if initError != nil {
return nil, initError
}

token, err := c.tokenCredential.GetToken(context.Background(), policy.TokenRequestOptions{
Scopes: []string{scope},
})
if err != nil {
return nil, err
}

return &Token{AccessToken: token.Token, ExpiresOn: token.ExpiresOn}, nil
}
25 changes: 25 additions & 0 deletions util/workloadidentity/workloadidentity_no_cgo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//go:build darwin && !cgo

// Package workloadidentity
// This file is used when the GOOS is darwin and CGO is not enabled.
// It provides a no-op implementation of the WorkloadIdentityTokenProvider to allow goreleaser to build
// a darwin binary on a linux machine.
package workloadidentity

import (
"errors"
)

type WorkloadIdentityTokenProvider struct {
}

const CGOError = "CGO is not enabled, cannot use workload identity token provider"

// Code that does not require CGO
func NewWorkloadIdentityTokenProvider() TokenProvider {
panic(CGOError)
}

func (c WorkloadIdentityTokenProvider) GetToken(scope string) (*Token, error) {
return nil, errors.New(CGOError)
}
Loading