added support for dockerconfigjson regcred
	
		
			
	
		
	
	
		
	
		
			Some checks failed
		
		
	
	
		
			
				
	
				continuous-integration/drone/push Build is failing
				
			
		
		
	
	
				
					
				
			
		
			Some checks failed
		
		
	
	continuous-integration/drone/push Build is failing
				
			This commit is contained in:
		
							
								
								
									
										20
									
								
								Dockerfile
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								Dockerfile
									
									
									
									
									
								
							@@ -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-upgrade -y; \
 | 
			
		||||
    apt-get install -y --no-install-recommends python3 python3-pip; \
 | 
			
		||||
    apt-get clean;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										39
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										39
									
								
								README.md
									
									
									
									
									
								
							@@ -45,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
 | 
			
		||||
@@ -83,6 +85,41 @@ 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
 | 
			
		||||
 
 | 
			
		||||
@@ -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}!")
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,6 @@
 | 
			
		||||
# Bitwarden CRD Operator
 | 
			
		||||
 | 
			
		||||
[](https://drone.uploadfilter24.eu/lerentis/bitwarden-crd-operator)
 | 
			
		||||
[](https://drone.uploadfilter24.eu/lerentis/bitwarden-crd-operator) [](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.
 | 
			
		||||
 | 
			
		||||
@@ -45,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
 | 
			
		||||
@@ -83,6 +85,41 @@ 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
 | 
			
		||||
 
 | 
			
		||||
@@ -12,7 +12,7 @@ spec:
 | 
			
		||||
    shortNames:
 | 
			
		||||
      - bws
 | 
			
		||||
  versions:
 | 
			
		||||
    - name: v1beta2
 | 
			
		||||
    - name: v1beta3
 | 
			
		||||
      served: true
 | 
			
		||||
      storage: true
 | 
			
		||||
      schema:
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										43
									
								
								charts/bitwarden-crd-operator/crds/registry-credentials.yaml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								charts/bitwarden-crd-operator/crds/registry-credentials.yaml
									
									
									
									
									
										Normal 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
 | 
			
		||||
@@ -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"]
 | 
			
		||||
 
 | 
			
		||||
@@ -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
									
								
							
							
						
						
									
										12
									
								
								example_dockerlogin.yaml
									
									
									
									
									
										Normal 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"
 | 
			
		||||
		Reference in New Issue
	
	Block a user