Skip to content

Commit 7a747e8

Browse files
Features/improved secrets loading (#92)
* New code functionality complete for Issue #60 * checkpoint * reconciler tests complete * Checkpoint with testfixture * Checkpoint for major test cleanup * first pass refactor of unit tests completed * All unit tests are functioning as inteded. * Well, sort of... fixed now. * final cleanup * Added necessary mods to support new metrics changes * updated controller-gen stuff to support build * Removed malfunctioning test * Updated Docker base image to go v1.23 * Minor updates per feedback
1 parent 87bcb50 commit 7a747e8

30 files changed

+2552
-1655
lines changed

.devcontainer/devcontainer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "BW k8s Operator",
3-
"image": "mcr.microsoft.com/devcontainers/go:1.21",
3+
"image": "mcr.microsoft.com/devcontainers/go:1.23",
44
"runArgs": ["--network=host"], // needed for kind
55
"postCreateCommand": "sudo .devcontainer/postCreateCommand.sh",
66
"customizations": {
@@ -22,7 +22,7 @@
2222
"ghcr.io/meaningful-ooo/devcontainer-features/fish:1": {
2323
"fisher": true
2424
},
25-
"ghcr.io/devcontainers-contrib/features/kind:1": {}
25+
"ghcr.io/devcontainers-extra/features/kind:1": {}
2626
},
2727
"secrets": {
2828
},
@@ -33,7 +33,8 @@
3333
"BWS_ACCESS_TOKEN": "${localEnv:BWS_ACCESS_TOKEN}",
3434
"BW_API_URL": "${localEnv:BW_API_URL}",
3535
"BW_IDENTITY_API_URL": "${localEnv:BW_IDENTITY_API_URL}",
36-
"BW_SECRETS_MANAGER_REFRESH_INTERVAL": "${localEnv:BW_SECRETS_MANAGER_REFRESH_INTERVAL}"
36+
"BW_SECRETS_MANAGER_REFRESH_INTERVAL": "${localEnv:BW_SECRETS_MANAGER_REFRESH_INTERVAL}",
37+
"GOFLAGS": "-ldflags=-extldflags=-lm"
3738
},
3839
"remoteUser": "root" // needed for kind: https://github.com/kubernetes-sigs/kind/issues/3196#issuecomment-1537260166
3940
}

.devcontainer/postCreateCommand.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
apt-get update
33
apt-get install -y kubernetes-client musl-tools # kubectl
44
kind delete cluster --name sm-operator && kind create cluster --name sm-operator --config .devcontainer/kind-config.yaml
5+
go install -mod=mod github.com/onsi/ginkgo/v2/ginkgo@latest
56

67
PATH="$PATH:/usr/local/go/bin" make setup
78
PATH="$PATH:/usr/local/go/bin" make install

.vscode/launch.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"mode": "debug",
1212
"program": "${workspaceFolder}/cmd/main.go",
1313
"cwd": "${workspaceFolder}",
14-
"buildFlags": ["-ldflags='-linkmode external -extldflags \"-static -Wl,-unresolved-symbols=ignore-all\"'"],
14+
"buildFlags": ["-ldflags=-linkmode external -extldflags \"-static -Wl,-unresolved-symbols=ignore-all\""],
1515
"envFile": "${workspaceFolder}/.env",
1616
"env": {
1717
"CC": "musl-gcc"
@@ -24,7 +24,7 @@
2424
"mode": "test",
2525
"program": "${relativeFileDirname}",
2626
"cwd": "${relativeFileDirname}",
27-
"buildFlags": ["-ldflags='-linkmode external -extldflags \"-static -Wl,-unresolved-symbols=ignore-all\"'"],
27+
"buildFlags": ["-ldflags=-linkmode external -extldflags '-static -Wl,-unresolved-symbols=ignore-all'"],
2828
"env": {
2929
"CC": "musl-gcc"
3030
}

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"remote.autoForwardPortsFallback": 0
3+
}

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.21 as builder
1+
FROM golang:1.23 as builder
22
ARG TARGETOS
33
ARG TARGETARCH
44

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ vet: ## Run go vet against code.
122122

123123
.PHONY: test
124124
test: manifests generate fmt vet envtest ## Run tests.
125-
CC=musl-gcc KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test -ldflags '-linkmode external -extldflags "-static -Wl,-unresolved-symbols=ignore-all"' ./... -coverprofile cover.out
125+
CC=musl-gcc KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test -ldflags '-linkmode external -extldflags "-static -Wl,-unresolved-symbols=ignore-all"' ./... -coverprofile cover.out -coverpkg=./...
126126

127127
##@ Build
128128

@@ -210,8 +210,8 @@ CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
210210
ENVTEST ?= $(LOCALBIN)/setup-envtest
211211

212212
## Tool Versions
213-
KUSTOMIZE_VERSION ?= v5.0.1
214-
CONTROLLER_TOOLS_VERSION ?= v0.12.0
213+
KUSTOMIZE_VERSION ?= v5.2.1
214+
CONTROLLER_TOOLS_VERSION ?= v0.14.0
215215

216216
.PHONY: kustomize
217217
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary. If wrong version is installed, it will be removed before downloading.

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,21 @@ Unit tests are currently found in the following files:
164164

165165
- cmd/suite_test.go
166166

167-
To run the unit tests, run `make test` from the root directory of this workspace. To debug the unit tests, click on the file you would like to debug. In the `Run and Debug` tab in Visual Studio Code, change the launch configuration from "Debug" to "Test current file", and then press F5. **NOTE: Using the Visual Studio Code "Testing" tab does not currently work due to VS Code not linking the static binaries correctly.**
167+
To run the unit tests, run `make test` from the root directory of this workspace. To debug the unit tests, click on the file you would like to debug. In the `Run and Debug` tab in Visual Studio Code, change the launch configuration from "Debug" to "Test current file", and then press F5.
168+
169+
**NOTE: Using the Visual Studio Code "Testing" tab may not work OOB due to VS Code not linking the static binaries correctly. The solution is to perform the following tasks***
170+
171+
Update VSCode Settings for Tests:
172+
173+
* Open VSCode settings (Ctrl+, or Cmd+,).
174+
* Search for go.testFlags.
175+
* Add the following to the go.testFlags array:
176+
177+
```json
178+
179+
["-ldflags=-extldflags=-lm"]
180+
181+
```
182+
183+
This tells the Go test runner to include the linker flag for all test commands.
184+

api/v1/bitwardensecret_types.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ type BitwardenSecretSpec struct {
4545
// The secret key reference for the authorization token used to connect to Secrets Manager
4646
// +kubebuilder:Required
4747
AuthToken AuthToken `json:"authToken"`
48+
// OnlyMappedSecrets, when true, restricts the Kubernetes Secret to only include secrets specified in SecretMap.
49+
// When false or unset, all secrets accessible by the machine account are included, with SecretMap applied for renaming.
50+
// Defaults to true.
51+
// +kubebuilder:validation:Optional
52+
// +kubebuilder:default=true
53+
OnlyMappedSecrets bool `json:"onlyMappedSecrets"`
4854
}
4955

5056
type AuthToken struct {

api/v1/zz_generated.deepcopy.go

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/main.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Trademark Guidelines
2222
package main
2323

2424
import (
25+
"crypto/tls"
2526
"flag"
2627
"fmt"
2728
"net/url"
@@ -40,6 +41,7 @@ import (
4041
ctrl "sigs.k8s.io/controller-runtime"
4142
"sigs.k8s.io/controller-runtime/pkg/healthz"
4243
"sigs.k8s.io/controller-runtime/pkg/log/zap"
44+
"sigs.k8s.io/controller-runtime/pkg/metrics/filters"
4345
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
4446

4547
operatorsv1 "github.com/bitwarden/sm-kubernetes/api/v1"
@@ -63,6 +65,8 @@ func main() {
6365
var metricsAddr string
6466
var enableLeaderElection bool
6567
var probeAddr string
68+
var enableHTTP2 bool
69+
var tlsOpts []func(*tls.Config)
6670

6771
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
6872
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
@@ -77,6 +81,28 @@ func main() {
7781

7882
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
7983

84+
// if the enable-http2 flag is false (the default), http/2 should be disabled
85+
// due to its vulnerabilities. More specifically, disabling http/2 will
86+
// prevent from being vulnerable to the HTTP/2 Stream Cancellation and
87+
// Rapid Reset CVEs. For more information see:
88+
// - https://github.com/advisories/GHSA-qppj-fm5r-hxr3
89+
// - https://github.com/advisories/GHSA-4374-p667-p6c8
90+
disableHTTP2 := func(c *tls.Config) {
91+
setupLog.Info("disabling http/2")
92+
c.NextProtos = []string{"http/1.1"}
93+
}
94+
95+
if !enableHTTP2 {
96+
tlsOpts = append(tlsOpts, disableHTTP2)
97+
}
98+
99+
metricsServerOptions := server.Options{
100+
BindAddress: metricsAddr,
101+
SecureServing: true,
102+
TLSOpts: tlsOpts,
103+
FilterProvider: filters.WithAuthenticationAndAuthorization,
104+
}
105+
80106
bwApiUrl, identApiUrl, statePath, refreshIntervalSeconds, err := GetSettings()
81107

82108
if err != nil {
@@ -86,10 +112,8 @@ func main() {
86112
bwClientFactory := controller.NewBitwardenClientFactory(*bwApiUrl, *identApiUrl)
87113

88114
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
89-
Scheme: scheme,
90-
Metrics: server.Options{
91-
BindAddress: metricsAddr,
92-
},
115+
Scheme: scheme,
116+
Metrics: metricsServerOptions,
93117
HealthProbeBindAddress: probeAddr,
94118
LeaderElection: enableLeaderElection,
95119
LeaderElectionID: "479cde60.bitwarden.com",

0 commit comments

Comments
 (0)