7 Commits

Author SHA1 Message Date
a2f248c6ff fixed typo
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
2022-10-30 00:39:50 +02:00
15a7258d99 bumping chart version and adding new crd
Some checks failed
continuous-integration/drone/push Build is failing
2022-10-30 00:38:44 +02:00
06efcf1a55 added support for dockerconfigjson regcred
Some checks failed
continuous-integration/drone/push Build is failing
2022-10-30 00:34:54 +02:00
2c9d1794a5 added Artifact Hub link
All checks were successful
continuous-integration/drone/push Build is passing
2022-10-29 21:32:09 +02:00
0fd7abdcc0 fixed kube version typo
All checks were successful
continuous-integration/drone/push Build is passing
2022-10-25 22:04:40 +02:00
11fb42769c added possibility to use an existing secret for deployment
All checks were successful
continuous-integration/drone/push Build is passing
2022-10-25 21:15:40 +02:00
aec384c78e Removed no longer needed dependency
All checks were successful
continuous-integration/drone/push Build is passing
2022-10-03 21:06:50 +00:00
13 changed files with 386 additions and 25 deletions

View File

@ -7,6 +7,25 @@ RUN apk add wget unzip
RUN cd /tmp && wget https://github.com/bitwarden/clients/releases/download/cli-v${BW_VERSION}/bw-linux-${BW_VERSION}.zip && \
unzip /tmp/bw-linux-${BW_VERSION}.zip
#FROM alpine:3.18 as run
#
#RUN set -eux; \
# groupadd -r bw-operator ; \
# useradd -r -g bw-operator -s /sbin/nologin bw-operator; \
# mkdir -p /home/bw-operator; \
# chown -R bw-operator /home/bw-operator; \
# chmod +x /usr/local/bin/bw; \
# apk add libstdc++ python3 py-pip
#COPY --chown=bw-operator:bw-operator bitwarden-crd-operator.py /home/bw-operator/bitwarden-crd-operator.py
#
#USER bw-operator
#
#RUN set -eux; \
# pip install -r requirements.txt --no-warn-script-location
#
#ENTRYPOINT [ "/home/bw-operator/.local/bin/kopf", "run", "--all-namespaces", "--liveness=http://0.0.0.0:8080/healthz" ]
#CMD [ "/home/bw-operator/bitwarden-crd-operator.py" ]
FROM ubuntu:jammy
COPY --from=builder /tmp/bw /usr/local/bin/bw
@ -19,6 +38,7 @@ RUN set -eux; \
chown -R bw-operator /home/bw-operator; \
chmod +x /usr/local/bin/bw; \
apt-get update; \
apt-get upgrade -y; \
apt-get install -y --no-install-recommends python3 python3-pip; \
apt-get clean;

View File

@ -1,6 +1,6 @@
# Bitwarden CRD Operator
[![Build Status](https://drone.uploadfilter24.eu/api/badges/lerentis/bitwarden-crd-operator/status.svg?ref=refs/heads/main)](https://drone.uploadfilter24.eu/lerentis/bitwarden-crd-operator)
[![Build Status](https://drone.uploadfilter24.eu/api/badges/lerentis/bitwarden-crd-operator/status.svg?ref=refs/heads/main)](https://drone.uploadfilter24.eu/lerentis/bitwarden-crd-operator) [![Artifact Hub](https://img.shields.io/endpoint?url=https://artifacthub.io/badge/repository/lerentis)](https://artifacthub.io/packages/search?repo=lerentis)
Bitwarden CRD Operator is a kubernetes Operator based on [kopf](https://github.com/nolar/kopf/). The goal is to create kubernetes native secret objects from bitwarden.
@ -9,8 +9,6 @@ Bitwarden CRD Operator is a kubernetes Operator based on [kopf](https://github.c
## Getting started
For now a few secrets need to be passed to helm. I will change this in the future to give the option to also use a kubernetes secret for this.
You will need a `ClientID` and `ClientSecret` ([where to get these](https://bitwarden.com/help/personal-api-key/)) as well as your password.
Expose these to the operator as described in this example:
@ -26,6 +24,16 @@ env:
value: "YourSuperSecurePassword"
```
you can also create a secret manually with these information and reference the existing secret like this in the `values.yaml`:
```yaml
externalConfigSecret:
enabled: true
name: "my-existing-secret"
```
the helm template will use all environment variables from this secret, so make sure to prepare this secret with the key value pairs as described above.
`BW_HOST` can be omitted if you are using the Bitwarden SaaS offering.
After that it is a basic helm deployment:
@ -37,11 +45,13 @@ kubectl create namespace bw-operator
helm upgrade --install --namespace bw-operator -f values.yaml bw-operator bitwarden-operator/bitwarden-crd-operator
```
## BitwardenSecret
And you are set to create your first secret using this operator. For that you need to add a CRD Object like this to your cluster:
```yaml
---
apiVersion: "lerentis.uploadfilter24.eu/v1beta2"
apiVersion: "lerentis.uploadfilter24.eu/v1beta3"
kind: BitwardenSecret
metadata:
name: name-of-your-management-object
@ -75,10 +85,45 @@ metadata:
type: Opaque
```
## RegistryCredential
For managing registry credentials, or pull secrets, you can create another kind of object to let the operator create these as well for you:
```yaml
---
apiVersion: "lerentis.uploadfilter24.eu/v1beta3"
kind: RegistryCredential
metadata:
name: name-of-your-management-object
spec:
usernameRef: nameOfTheFieldInBitwarden # for example username
passwordRef: nameOfTheFieldInBitwarden # for example password
registry: "docker.io"
id: "A Secret ID from bitwarden"
name: "Name of the secret to be created"
namespace: "Namespace of the secret to be created"
```
The resulting secret looks something like this:
```yaml
apiVersion: v1
data:
.dockerconfigjson: "base64 encoded json auth string for your registry"
kind: Secret
metadata:
annotations:
managed: bitwarden-secrets.lerentis.uploadfilter24.eu
managedObject: bw-operator/test
name: name-of-your-management-object
namespace: default
type: dockerconfigjson
```
## Short Term Roadmap
- [ ] support more types
- [ ] offer option to use a existing secret in helm chart
- [x] offer option to use a existing secret in helm chart
- [x] host chart on gh pages
- [x] write release pipeline
- [x] maybe extend spec to offer modification of keys as well

View File

@ -9,6 +9,7 @@ import json
from pprint import pprint
def get_secret_from_bitwarden(logger, id):
logger.info(f"Locking up secret with ID: {id}")
return command_wrapper(logger, f"get item {id}")
def unlock_bw(logger):
@ -25,6 +26,37 @@ def command_wrapper(logger, command):
logger.warn(f"Error during bw cli invokement: {err}")
return out.decode(encoding='UTF-8')
def create_kv(secret, secret_json, content_def):
secret.type = "Opaque"
secret.data = {}
for eleml in content_def:
for k, elem in eleml.items():
for key,value in elem.items():
if key == "secretName":
_secret_key = value
if key == "secretRef":
_secret_ref = value
secret.data[_secret_ref] = str(base64.b64encode(secret_json["login"][_secret_key].encode("utf-8")), "utf-8")
return secret
def create_dockerlogin(logger, secret, secret_json, username_ref, password_ref, registry):
secret.type = "dockerconfigjson"
secret.data = {}
auths_dict = {}
registry_dict = {}
reg_auth_dict = {}
_username = secret_json["login"][username_ref]
logger.info(f"Creating login with username: {_username}")
_password = secret_json["login"][password_ref]
cred_field = str(base64.b64encode(f"{_username}:{_password}".encode("utf-8")), "utf-8")
reg_auth_dict["auth"] = cred_field
registry_dict[registry] = reg_auth_dict
auths_dict["auths"] = registry_dict
secret.data[".dockerconfigjson"] = str(base64.b64encode(json.dumps(auths_dict).encode("utf-8")), "utf-8")
return secret
@kopf.on.startup()
def bitwarden_signin(logger, **kwargs):
if 'BW_HOST' in os.environ:
@ -34,6 +66,35 @@ def bitwarden_signin(logger, **kwargs):
command_wrapper(logger, "login --apikey")
unlock_bw(logger)
@kopf.on.create('registry-credentials.lerentis.uploadfilter24.eu')
def create_managed_registry_secret(spec, name, namespace, logger, body, **kwargs):
username_ref = spec.get('usernameRef')
password_ref = spec.get('passwordRef')
registry = spec.get('registry')
id = spec.get('id')
secret_name = spec.get('name')
secret_namespace = spec.get('namespace')
unlock_bw(logger)
secret_json_object = json.loads(get_secret_from_bitwarden(logger, id))
api = kubernetes.client.CoreV1Api()
annotations = {
"managed": "registry-credentials.lerentis.uploadfilter24.eu",
"managedObject": f"{namespace}/{name}"
}
secret = kubernetes.client.V1Secret()
secret.metadata = kubernetes.client.V1ObjectMeta(name=secret_name, annotations=annotations)
secret = create_dockerlogin(logger, secret, secret_json_object, username_ref, password_ref, registry)
obj = api.create_namespaced_secret(
secret_namespace, secret
)
logger.info(f"Registry Secret {secret_namespace}/{secret_name} has been created")
@kopf.on.create('bitwarden-secrets.lerentis.uploadfilter24.eu')
def create_managed_secret(spec, name, namespace, logger, body, **kwargs):
@ -54,23 +115,13 @@ def create_managed_secret(spec, name, namespace, logger, body, **kwargs):
}
secret = kubernetes.client.V1Secret()
secret.metadata = kubernetes.client.V1ObjectMeta(name=secret_name, annotations=annotations)
secret.type = "Opaque"
secret.data = {}
for eleml in content_def:
for k, elem in eleml.items():
for key,value in elem.items():
if key == "secretName":
_secret_key = value
if key == "secretRef":
_secret_ref = value
secret.data[_secret_ref] = str(base64.b64encode(secret_json_object["login"][_secret_key].encode("utf-8")), "utf-8")
secret = create_kv(secret, secret_json_object, content_def)
obj = api.create_namespaced_secret(
secret_namespace, secret
)
logger.info(f"Secret {secret_namespace}/{secret_name} is created")
logger.info(f"Secret {secret_namespace}/{secret_name} has been created")
@kopf.on.update('bitwarden-secrets.lerentis.uploadfilter24.eu')
@ -88,3 +139,15 @@ def delete_managed_secret(spec, name, namespace, logger, **kwargs):
logger.info(f"Secret {secret_namespace}/{secret_name} has been deleted")
except:
logger.warn(f"Could not delete secret {secret_namespace}/{secret_name}!")
@kopf.on.delete('registry-credentials.lerentis.uploadfilter24.eu')
def delete_managed_secret(spec, name, namespace, logger, **kwargs):
secret_name = spec.get('name')
secret_namespace = spec.get('namespace')
api = kubernetes.client.CoreV1Api()
try:
api.delete_namespaced_secret(secret_name, secret_namespace)
logger.info(f"Secret {secret_namespace}/{secret_name} has been deleted")
except:
logger.warn(f"Could not delete secret {secret_namespace}/{secret_name}!")

View File

@ -4,6 +4,46 @@ description: Deploy the Bitwarden CRD Operator
type: application
version: "v0.1.2"
version: "v0.3.0"
appVersion: "0.1.2"
appVersion: "0.2.0"
keywords:
- operator
- bitwarden
- vaultwarden
home: https://lerentis.github.io/bitwarden-crd-operator/
sources:
- https://github.com/Lerentis/bitwarden-crd-operator
kubeVersion: '>= 1.23.0-0'
maintainers:
- name: lerentis
email: lerentis+helm@uploadfilter24.eu
annotations:
artifacthub.io/links: |
- name: Chart Source
url: https://github.com/Lerentis/bitwarden-crd-operator
artifacthub.io/crds: |
- kind: BitwardenSecret
version: v1beta3
name: bitwarden-secret
displayName: Bitwarden Secret
description: Management Object to create secrets from bitwarden
- kind: RegistryCredential
version: v1beta3
name: registry-credential
displayName: Regestry Credentials
description: Management Object to create regestry secrets from bitwarden
artifacthub.io/license: MIT
artifacthub.io/operator: "true"
artifacthub.io/changes: |
- kind: added
description: "Added support for regestry credentials"
artifacthub.io/images: |
- name: bitwarden-crd-operator
image: lerentis/bitwarden-crd-operator:0.2.0

View File

@ -0,0 +1,129 @@
# Bitwarden CRD Operator
[![Build Status](https://drone.uploadfilter24.eu/api/badges/lerentis/bitwarden-crd-operator/status.svg?ref=refs/heads/main)](https://drone.uploadfilter24.eu/lerentis/bitwarden-crd-operator) [![Artifact Hub](https://img.shields.io/endpoint?url=https://artifacthub.io/badge/repository/lerentis)](https://artifacthub.io/packages/search?repo=lerentis)
Bitwarden CRD Operator is a kubernetes Operator based on [kopf](https://github.com/nolar/kopf/). The goal is to create kubernetes native secret objects from bitwarden.
> DISCLAIMER:
> This project is still very work in progress :)
## Getting started
You will need a `ClientID` and `ClientSecret` ([where to get these](https://bitwarden.com/help/personal-api-key/)) as well as your password.
Expose these to the operator as described in this example:
```yaml
env:
- name: BW_HOST
value: "https://bitwarden.your.tld.org"
- name: BW_CLIENTID
value: "user.your-client-id"
- name: BW_CLIENTSECRET
value: "YoUrCliEntSecRet"
- name: BW_PASSWORD
value: "YourSuperSecurePassword"
```
you can also create a secret manually with these information and reference the existing secret like this in the `values.yaml`:
```yaml
externalConfigSecret:
enabled: true
name: "my-existing-secret"
```
the helm template will use all environment variables from this secret, so make sure to prepare this secret with the key value pairs as described above.
`BW_HOST` can be omitted if you are using the Bitwarden SaaS offering.
After that it is a basic helm deployment:
```bash
helm repo add bitwarden-operator https://lerentis.github.io/bitwarden-crd-operator
helm repo update
kubectl create namespace bw-operator
helm upgrade --install --namespace bw-operator -f values.yaml bw-operator bitwarden-operator/bitwarden-crd-operator
```
## BitwardenSecret
And you are set to create your first secret using this operator. For that you need to add a CRD Object like this to your cluster:
```yaml
---
apiVersion: "lerentis.uploadfilter24.eu/v1beta3"
kind: BitwardenSecret
metadata:
name: name-of-your-management-object
spec:
content:
- element:
secretName: nameOfTheFieldInBitwarden # for example username
secretRef: nameOfTheKeyInTheSecretToBeCreated
- element:
secretName: nameOfAnotherFieldInBitwarden # for example password
secretRef: nameOfAnotherKeyInTheSecretToBeCreated
id: "A Secret ID from bitwarden"
name: "Name of the secret to be created"
namespace: "Namespace of the secret to be created"
```
The ID can be extracted from the browser when you open a item the ID is in the URL. The resulting secret looks something like this:
```yaml
apiVersion: v1
data:
nameOfTheKeyInTheSecretToBeCreated: "base64 encoded value of TheFieldInBitwarden"
nameOfAnotherKeyInTheSecretToBeCreated: "base64 encoded value of AnotherFieldInBitwarden"
kind: Secret
metadata:
annotations:
managed: bitwarden-secrets.lerentis.uploadfilter24.eu
managedObject: bw-operator/test
name: name-of-your-management-object
namespace: default
type: Opaque
```
## RegistryCredential
For managing registry credentials, or pull secrets, you can create another kind of object to let the operator create these as well for you:
```yaml
---
apiVersion: "lerentis.uploadfilter24.eu/v1beta3"
kind: RegistryCredential
metadata:
name: name-of-your-management-object
spec:
usernameRef: nameOfTheFieldInBitwarden # for example username
passwordRef: nameOfTheFieldInBitwarden # for example password
registry: "docker.io"
id: "A Secret ID from bitwarden"
name: "Name of the secret to be created"
namespace: "Namespace of the secret to be created"
```
The resulting secret looks something like this:
```yaml
apiVersion: v1
data:
.dockerconfigjson: "base64 encoded json auth string for your registry"
kind: Secret
metadata:
annotations:
managed: bitwarden-secrets.lerentis.uploadfilter24.eu
managedObject: bw-operator/test
name: name-of-your-management-object
namespace: default
type: dockerconfigjson
```
## Short Term Roadmap
- [ ] support more types
- [x] offer option to use a existing secret in helm chart
- [x] host chart on gh pages
- [x] write release pipeline
- [x] maybe extend spec to offer modification of keys as well

View File

@ -12,7 +12,7 @@ spec:
shortNames:
- bws
versions:
- name: v1beta2
- name: v1beta3
served: true
storage: true
schema:

View File

@ -0,0 +1,43 @@
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: registry-credentials.lerentis.uploadfilter24.eu
spec:
scope: Namespaced
group: lerentis.uploadfilter24.eu
names:
kind: RegistryCredential
plural: registry-credentials
singular: registry-credential
shortNames:
- rgc
versions:
- name: v1beta3
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
usernameRef:
type: string
passwordRef:
type: string
registry:
type: string
id:
type: string
namespace:
type: string
name:
type: string
required:
- id
- namespace
- name
- usernameRef
- passwordRef
- registry

View File

@ -4,7 +4,7 @@ metadata:
name: {{ include "bitwarden-crd-operator.serviceAccountName" . }}-role
rules:
- apiGroups: ["lerentis.uploadfilter24.eu"]
resources: ["bitwarden-secrets"]
resources: ["bitwarden-secrets", "registry-credentials"]
verbs: ["get", "watch", "list", "create", "delete", "patch", "update"]
- apiGroups: [""]
resources: ["secrets"]

View File

@ -33,10 +33,15 @@ spec:
{{- toYaml .Values.securityContext | nindent 12 }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
{{- with .Values.env }}
env:
{{- with .Values.env }}
{{- . | toYaml | trim | nindent 12 }}
{{- end }}
{{- if .Values.externalConfigSecret.enabled }}
envFrom:
- secretRef:
name: {{ .Values.externalConfigSecret.name }}
{{- end }}
ports:
- name: http
containerPort: 8080

View File

@ -24,6 +24,11 @@ fullnameOverride: ""
# - name: BW_PASSWORD
# value: "define_id"
externalConfigSecret:
enabled: false
name: ""
serviceAccount:
# Specifies whether a service account should be created
create: true

View File

@ -1,5 +1,5 @@
---
apiVersion: "lerentis.uploadfilter24.eu/v1beta2"
apiVersion: "lerentis.uploadfilter24.eu/v1beta3"
kind: BitwardenSecret
metadata:
name: test

12
example_dockerlogin.yaml Normal file
View File

@ -0,0 +1,12 @@
---
apiVersion: "lerentis.uploadfilter24.eu/v1beta3"
kind: RegistryCredential
metadata:
name: test
spec:
usernameRef: "username"
passwordRef: "password"
registry: "docker.io"
id: "3b249ec7-9ce7-440a-9558-f34f3ab10680"
name: "test-regcred"
namespace: "default"

View File

@ -1,3 +1,2 @@
kopf
kubernetes
jinja2
kubernetes