Skip to content

Commit f5afc52

Browse files
committed
Maintenance
- Test Docker Build in Continuous Integration GitHub Action - Change triggers for Continuous Integration GitHub Action - Adjust Helm install command - Format YAML files - Add linting
1 parent 4ce4bd5 commit f5afc52

File tree

10 files changed

+100
-35
lines changed

10 files changed

+100
-35
lines changed

.github/FUNDING.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
---
12
github: [ricoberger]
23
custom: ["https://www.paypal.me/ricoberger"]

.github/release.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
---
12
name-template: "$RESOLVED_VERSION"
23
tag-template: "$RESOLVED_VERSION"
34
version-template: "v$MAJOR.$MINOR.$PATCH"

.github/workflows/continuous-delivery.yaml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,6 @@ jobs:
3939
- name: Set up Docker Buildx
4040
uses: docker/setup-buildx-action@v3
4141

42-
- name: Login to DockerHub
43-
uses: docker/login-action@v3
44-
with:
45-
username: ${{ secrets.DOCKER_USERNAME }}
46-
password: ${{ secrets.DOCKER_PASSWORD }}
47-
4842
- name: Login to GitHub Container Registry
4943
uses: docker/login-action@v3
5044
with:

.github/workflows/continuous-integration.yaml

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1+
---
12
name: Continuous Integration
23

3-
on: pull_request
4+
on:
5+
push:
6+
branches:
7+
- main
8+
pull_request:
9+
branches:
10+
- main
411

512
jobs:
613
kubernetes:
@@ -65,9 +72,8 @@ jobs:
6572
cache: true
6673
cache-dependency-path: go.sum
6774

68-
- name: Download Dependencies
69-
run: |
70-
go mod download
75+
- name: Lint
76+
uses: golangci/golangci-lint-action@v7
7177

7278
- name: Test
7379
run: |
@@ -76,3 +82,27 @@ jobs:
7682
- name: Build
7783
run: |
7884
make build
85+
86+
docker:
87+
name: Docker
88+
runs-on: ubuntu-latest
89+
steps:
90+
- name: Checkout Code
91+
uses: actions/checkout@v4
92+
with:
93+
fetch-depth: 0
94+
95+
- name: Set up QEMU
96+
uses: docker/setup-qemu-action@v3
97+
98+
- name: Set up Docker Buildx
99+
uses: docker/setup-buildx-action@v3
100+
101+
- name: Build Docker Image
102+
id: docker_build
103+
uses: docker/build-push-action@v6
104+
with:
105+
push: false
106+
context: .
107+
file: ./Dockerfile
108+
platforms: linux/amd64

.github/workflows/release.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
---
12
name: Release
23

34
on:

.golangci.yaml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
version: "2"
3+
linters:
4+
default: none
5+
enable:
6+
- bodyclose
7+
- gosec
8+
- govet
9+
- ineffassign
10+
- noctx
11+
- staticcheck
12+
- unused
13+
- whitespace
14+
exclusions:
15+
generated: lax
16+
presets:
17+
- comments
18+
- common-false-positives
19+
- legacy
20+
- std-error-handling
21+
paths:
22+
- third_party$
23+
- builtin$
24+
- examples$
25+
formatters:
26+
enable:
27+
- gofmt
28+
- goimports
29+
settings:
30+
goimports:
31+
local-prefixes:
32+
- github.com/ricoberger/script_exporter
33+
exclusions:
34+
generated: lax
35+
paths:
36+
- third_party$
37+
- builtin$
38+
- examples$

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ running at `http://vault:8200`, but can be overidden by specifying
2828
`--set vault.address=https://vault.example.com`
2929

3030
```sh
31-
helm upgrade --install vault-secrets-operator oci://ghcr.io/ricoberger/charts/vault-secrets-operator --version 3.0.0
31+
helm upgrade --install vault-secrets-operator oci://ghcr.io/ricoberger/charts/vault-secrets-operator --version <VERSION>
3232
```
3333

3434
### Prepare Vault

controllers/vaultsecret_controller.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,17 @@ func (r *VaultSecretReconciler) Reconcile(ctx context.Context, req ctrl.Request)
150150
return ctrl.Result{}, nil
151151
}
152152

153-
if instance.Spec.SecretEngine == "" || instance.Spec.SecretEngine == kvEngine {
153+
switch instance.Spec.SecretEngine {
154+
case "", kvEngine:
154155
data, err = vaultClient.GetSecret(instance.Spec.SecretEngine, instance.Spec.Path, instance.Spec.Keys, instance.Spec.Version, instance.Spec.IsBinary, instance.Spec.VaultNamespace)
155156
if err != nil {
156157
// Error while getting the secret from Vault - requeue the request.
157158
log.Error(err, "Could not get secret from vault")
158159
r.updateConditions(ctx, instance, conditionReasonFetchFailed, err.Error(), metav1.ConditionFalse)
159160
return ctrl.Result{}, err
160161
}
161-
} else if instance.Spec.SecretEngine == pkiEngine {
162+
163+
case pkiEngine:
162164
if err := ValidatePKI(instance); err != nil {
163165
log.Error(err, "Resource validation failed")
164166
r.updateConditions(ctx, instance, conditionReasonInvalidResource, err.Error(), metav1.ConditionFalse)
@@ -408,8 +410,8 @@ func newSecretForCR(cr *ricobergerdev1alpha1.VaultSecret, data map[string][]byte
408410
ObjectMeta: metav1.ObjectMeta{
409411
Name: cr.Name,
410412
Namespace: cr.Namespace,
411-
Labels: cr.ObjectMeta.Labels,
412-
Annotations: cr.ObjectMeta.Annotations,
413+
Labels: cr.Labels,
414+
Annotations: cr.Annotations,
413415
},
414416
Data: data,
415417
Type: cr.Spec.Type,

vault/client.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (c *Client) RenewToken() {
6565

6666
// Request a new token if the actual token lifetime more than the specified maximum
6767
// lifetime.
68-
elapsed := time.Now().Sub(started).Seconds()
68+
elapsed := time.Since(started).Seconds()
6969
if c.tokenMaxTTL > 0 && elapsed >= float64(c.tokenMaxTTL) && c.requestToken != nil {
7070
log.Info("Request new Vault token")
7171
err := c.requestToken(c)
@@ -97,7 +97,7 @@ func (c *Client) RenewToken() {
9797
// GetHealth checks if the failedRenewTokenAttempts hits the given thresholds. If this is the case an error is returned.
9898
func (c *Client) GetHealth(threshold int) error {
9999
if c.failedRenewTokenAttempts >= threshold {
100-
return fmt.Errorf("Renew Vault token failed %d times", c.failedRenewTokenAttempts)
100+
return fmt.Errorf("renew Vault token failed %d times", c.failedRenewTokenAttempts)
101101
}
102102

103103
return nil
@@ -214,7 +214,7 @@ func convertData(secretData map[string]interface{}, keys []string, isBinary bool
214214
continue
215215
}
216216
if len(keys) == 0 || contains(key, keys) {
217-
switch value.(type) {
217+
switch value := value.(type) {
218218
case map[string]interface{}:
219219
jsonString, err := json.Marshal(value)
220220
if err != nil {
@@ -223,17 +223,17 @@ func convertData(secretData map[string]interface{}, keys []string, isBinary bool
223223
data[key] = []byte(jsonString)
224224
case string:
225225
if isBinary {
226-
data[key], err = b64.StdEncoding.DecodeString(value.(string))
226+
data[key], err = b64.StdEncoding.DecodeString(value)
227227
if err != nil {
228228
return nil, err
229229
}
230230
} else {
231-
data[key] = []byte(value.(string))
231+
data[key] = []byte(value)
232232
}
233233
case json.Number:
234-
data[key] = []byte(value.(json.Number))
234+
data[key] = []byte(value)
235235
case bool:
236-
data[key] = []byte(fmt.Sprintf("%t", value.(bool)))
236+
data[key] = []byte(fmt.Sprintf("%t", value))
237237
default:
238238
return nil, fmt.Errorf("could not parse secret value")
239239
}
@@ -257,8 +257,7 @@ func (c *Client) kvPreflightVersionRequest(path string) (string, int, error) {
257257
c.client.SetOutputCurlString(false)
258258
defer c.client.SetOutputCurlString(currentOutputCurlString)
259259

260-
r := c.client.NewRequest("GET", "/v1/sys/internal/ui/mounts/"+path)
261-
resp, err := c.client.RawRequest(r)
260+
resp, err := c.client.Logical().ReadRaw("/v1/sys/internal/ui/mounts/" + path)
262261
if resp != nil {
263262
defer resp.Body.Close()
264263
}

vault/vault.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ import (
66
"encoding/base64"
77
"encoding/json"
88
"fmt"
9-
"io/ioutil"
9+
"io"
1010
"os"
1111
"strconv"
1212
"time"
1313

1414
gcpmetadata "cloud.google.com/go/compute/metadata"
1515
gcpcredentials "cloud.google.com/go/iam/credentials/apiv1"
16+
gcpcredentialspb "cloud.google.com/go/iam/credentials/apiv1/credentialspb"
1617
"github.com/aws/aws-sdk-go/aws"
1718
awscredentials "github.com/aws/aws-sdk-go/aws/credentials"
1819
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
@@ -26,7 +27,6 @@ import (
2627
"github.com/pkg/errors"
2728
"golang.org/x/oauth2/google"
2829
"google.golang.org/api/iam/v1"
29-
gcpcredentialspb "google.golang.org/genproto/googleapis/iam/credentials/v1"
3030
logf "sigs.k8s.io/controller-runtime/pkg/log"
3131
)
3232

@@ -143,7 +143,7 @@ func CreateClient(vaultKubernetesRole string) (*Client, error) {
143143
return nil, fmt.Errorf("missing vault token")
144144
}
145145

146-
t, err := ioutil.ReadFile(vaultTokenPath)
146+
t, err := os.ReadFile(vaultTokenPath)
147147
if err != nil {
148148
return nil, err
149149
}
@@ -199,6 +199,7 @@ func CreateClient(vaultKubernetesRole string) (*Client, error) {
199199
return nil, nil
200200
}
201201

202+
// #nosec G101
202203
serviceAccountTokenPath := "/var/run/secrets/kubernetes.io/serviceaccount/token"
203204

204205
if vaultTokenPath != "" {
@@ -207,7 +208,7 @@ func CreateClient(vaultKubernetesRole string) (*Client, error) {
207208

208209
// Read the service account token value and create a map for the
209210
// authentication against Vault.
210-
kubeToken, err := ioutil.ReadFile(serviceAccountTokenPath)
211+
kubeToken, err := os.ReadFile(serviceAccountTokenPath)
211212
if err != nil {
212213
return nil, err
213214
}
@@ -478,7 +479,6 @@ func CreateClient(vaultKubernetesRole string) (*Client, error) {
478479
restrictNamespace: vaultRestrictNamespace,
479480
pkiRenew: pkiRenew,
480481
}, nil
481-
482482
}
483483

484484
if vaultAuthMethod == "aws" {
@@ -508,7 +508,7 @@ func CreateClient(vaultKubernetesRole string) (*Client, error) {
508508
return nil, fmt.Errorf("error requesting signature: %w", err)
509509
}
510510

511-
kubeToken, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/token")
511+
kubeToken, err := os.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/token")
512512
if err != nil {
513513
return nil, err
514514
}
@@ -539,7 +539,7 @@ func CreateClient(vaultKubernetesRole string) (*Client, error) {
539539
if err != nil {
540540
return nil, errors.Wrap(err, "error creating a new session to create a WebIdentityRoleProvider")
541541
}
542-
webIdentityProvider := stscreds.NewWebIdentityRoleProvider(sts.New(sess), roleARN, roleSessionName, tokenPath)
542+
webIdentityProvider := stscreds.NewWebIdentityRoleProviderWithOptions(sts.New(sess), roleARN, roleSessionName, stscreds.FetchTokenPath(tokenPath))
543543

544544
// Add the web identity role credential provider
545545
providers = append(providers, webIdentityProvider)
@@ -582,7 +582,7 @@ func CreateClient(vaultKubernetesRole string) (*Client, error) {
582582
if err != nil {
583583
return nil, err
584584
}
585-
requestBody, err := ioutil.ReadAll(stsRequest.HTTPRequest.Body)
585+
requestBody, err := io.ReadAll(stsRequest.HTTPRequest.Body)
586586
if err != nil {
587587
return nil, err
588588
}
@@ -672,7 +672,6 @@ func CreateClient(vaultKubernetesRole string) (*Client, error) {
672672
}
673673

674674
if vaultAuthMethod == "gcp" {
675-
676675
// Check the required mount path and role for the GCP Auth
677676
// Method. If one of the env variable is missing we return an error.
678677
if vaultGcpPath == "" {
@@ -711,7 +710,7 @@ func CreateClient(vaultKubernetesRole string) (*Client, error) {
711710

712711
if vaultGcpServiceAccountEmail == "" {
713712
metadataClient := gcpmetadata.NewClient(nil)
714-
vaultGcpServiceAccountEmail, err = metadataClient.Email("default")
713+
vaultGcpServiceAccountEmail, err = metadataClient.EmailWithContext(context.Background(), "default")
715714
if err != nil {
716715
return nil, fmt.Errorf("could not obtain service account from credentials; a service account to authenticate as must be provided")
717716
}
@@ -842,7 +841,7 @@ func setVaultIDs(idType string) string {
842841
idPath = os.Getenv("VAULT_SECRET_ID_PATH")
843842
}
844843

845-
id, err := ioutil.ReadFile(idPath)
844+
id, err := os.ReadFile(idPath)
846845
if err != nil {
847846
log.WithValues("VaultFilePath", idPath).Error(err, "missing secret vault-secrets-operator or bad path in volume")
848847
return string(id)

0 commit comments

Comments
 (0)