Skip to content

Commit

Permalink
Updating path for fetching 1password items to be of the op:// referen…
Browse files Browse the repository at this point in the history
…ce format
  • Loading branch information
jillianwilson committed Sep 6, 2021
1 parent 49d984c commit bb228ca
Show file tree
Hide file tree
Showing 17 changed files with 131 additions and 118 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

The 1Password Connect Kubernetes Operator provides the ability to integrate Kubernetes with 1Password. This Operator manages `OnePasswordItem` Custom Resource Definitions (CRDs) that define the location of an Item stored in 1Password. The `OnePasswordItem` CRD, when created, will be used to compose a Kubernetes Secret containing the contents of the specified item.

The 1Password Connect Kubernetes Operator also allows for Kubernetes Secrets to be composed from a 1Password Item through annotation of an Item Path on a deployment.
The 1Password Connect Kubernetes Operator also allows for Kubernetes Secrets to be composed from a 1Password Item through annotation of an Item Reference on a deployment.

The 1Password Connect Kubernetes Operator will continually check for updates from 1Password for any Kubernetes Secret that it has generated. If a Kubernetes Secret is updated, any Deployment using that secret can be automatically restarted.

Expand Down Expand Up @@ -106,7 +106,7 @@ kind: OnePasswordItem
metadata:
name: <item_name> #this name will also be used for naming the generated kubernetes secret
spec:
itemPath: "vaults/<vault_id_or_title>/items/<item_id_or_title>"
itemReference: "op://<vault_id_or_title>/<item_id_or_title>"
```
Deploy the OnePasswordItem to Kubernetes:
Expand All @@ -131,20 +131,20 @@ kind: Deployment
metadata:
name: deployment-example
annotations:
operator.1password.io/item-path: "vaults/<vault_id_or_title>/items/<item_id_or_title>"
operator.1password.io/item-reference: "op://<vault>/<item>"
operator.1password.io/item-name: "<secret_name>"
```
Applying this yaml file will create a Kubernetes Secret with the name `<secret_name>` and contents from the location specified at the specified Item Path.
Applying this yaml file will create a Kubernetes Secret with the name `<secret_name>` and contents from the location specified at the specified Item Reference.

Note: Deleting the Deployment that you've created will automatically delete the created Kubernetes Secret only if the deployment is still annotated with `operator.1password.io/item-path` and `operator.1password.io/item-name` and no other deployment is using the secret.
Note: Deleting the Deployment that you've created will automatically delete the created Kubernetes Secret only if the deployment is still annotated with `operator.1password.io/item-reference` and `operator.1password.io/item-name` and no other deployment is using the secret.

If a 1Password Item that is linked to a Kubernetes Secret is updated within the POLLING_INTERVAL the associated Kubernetes Secret will be updated. However, if you do not want a specific secret to be updated you can add the tag `operator.1password.io:ignore-secret` to the item stored in 1Password. While this tag is in place, any updates made to an item will not trigger an update to the associated secret in Kubernetes.

---
**NOTE**

If multiple 1Password vaults/items have the same `title` when using a title in the access path, the desired action will be performed on the oldest vault/item.
If multiple 1Password vaults/items have the same `title` when using a title in the access reference, the desired action will be performed on the oldest vault/item.

Titles and field names that include white space and other characters that are not a valid [DNS subdomain name](https://kubernetes.io/docs/concepts/configuration/secret/) will create Kubernetes secrets that have titles and fields in the following format:
- Invalid characters before the first alphanumeric character and after the last alphanumeric character will be removed
Expand Down
3 changes: 2 additions & 1 deletion cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ func main() {
ticker.Stop()
return
case <-ticker.C:
updatedSecretsPoller.UpdateKubernetesSecretsTask()
err := updatedSecretsPoller.UpdateKubernetesSecretsTask()
log.Error(err, "Error occured during update secret task")
}
}
}()
Expand Down
2 changes: 1 addition & 1 deletion deploy/crds/onepassword.com_onepassworditems_crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ spec:
spec:
description: OnePasswordItemSpec defines the desired state of OnePasswordItem
properties:
itemPath:
itemReference:
type: string
type: object
status:
Expand Down
2 changes: 1 addition & 1 deletion deploy/crds/onepassword.com_v1_onepassworditem_cr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ kind: OnePasswordItem
metadata:
name: example
spec:
itemPath: "vaults/<vault_id>/items/<item_id>"
itemReference: "op://<vault_id>/<item_id>"
5 changes: 3 additions & 2 deletions deploy/operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ spec:
serviceAccountName: onepassword-connect-operator
containers:
- name: onepassword-connect-operator
image: 1password/onepassword-operator
image: jillianagilebits/operator:v0.0.2
imagePullPolicy: Never
command: ["/manager"]
env:
- name: WATCH_NAMESPACE
Expand All @@ -36,4 +37,4 @@ spec:
name: onepassword-token
key: token
- name: AUTO_RESTART
value: "false"
value: "true"
2 changes: 1 addition & 1 deletion pkg/apis/onepassword/v1/onepasswordsecret_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

// OnePasswordItemSpec defines the desired state of OnePasswordItem
type OnePasswordItemSpec struct {
ItemPath string `json:"itemPath,omitempty"`
ItemReference string `json:"itemReference,omitempty"`
}

// OnePasswordItemStatus defines the observed state of OnePasswordItem
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/deployment/deployment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,11 @@ func (r *ReconcileDeployment) HandleApplyingDeployment(namespace string, annotat

secretName := annotations[op.NameAnnotation]
if len(secretName) == 0 {
reqLog.Info("No 'item-name' annotation set. 'item-path' and 'item-name' must be set as annotations to add new secret.")
reqLog.Info("No 'item-name' annotation set. 'item-reference' and 'item-name' must be set as annotations to add new secret.")
return nil
}

item, err := op.GetOnePasswordItemByPath(r.opConnectClient, annotations[op.ItemPathAnnotation])
item, err := op.GetOnePasswordItemByReference(r.opConnectClient, annotations[op.ItemReferenceAnnotation])
if err != nil {
return fmt.Errorf("Failed to retrieve item: %v", err)
}
Expand Down
34 changes: 17 additions & 17 deletions pkg/controller/deployment/deployment_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var (
"password": []byte(password),
"username": []byte(username),
}
itemPath = fmt.Sprintf("vaults/%v/items/%v", vaultId, itemId)
ItemReference = fmt.Sprintf("op://%v/%v", vaultId, itemId)
)

var (
Expand All @@ -76,8 +76,8 @@ var tests = []testReconcileItem{
finalizer,
},
Annotations: map[string]string{
op.ItemPathAnnotation: itemPath,
op.NameAnnotation: name,
op.ItemReferenceAnnotation: ItemReference,
op.NameAnnotation: name,
},
},
},
Expand All @@ -90,8 +90,8 @@ var tests = []testReconcileItem{
Name: "another-deployment",
Namespace: namespace,
Annotations: map[string]string{
op.ItemPathAnnotation: itemPath,
op.NameAnnotation: name,
op.ItemReferenceAnnotation: ItemReference,
op.NameAnnotation: name,
},
},
Spec: appsv1.DeploymentSpec{
Expand Down Expand Up @@ -152,8 +152,8 @@ var tests = []testReconcileItem{
finalizer,
},
Annotations: map[string]string{
op.ItemPathAnnotation: itemPath,
op.NameAnnotation: name,
op.ItemReferenceAnnotation: ItemReference,
op.NameAnnotation: name,
},
},
},
Expand All @@ -166,8 +166,8 @@ var tests = []testReconcileItem{
Name: "another-deployment",
Namespace: namespace,
Annotations: map[string]string{
op.ItemPathAnnotation: itemPath,
op.NameAnnotation: name,
op.ItemReferenceAnnotation: ItemReference,
op.NameAnnotation: name,
},
},
Spec: appsv1.DeploymentSpec{
Expand Down Expand Up @@ -235,8 +235,8 @@ var tests = []testReconcileItem{
finalizer,
},
Annotations: map[string]string{
op.ItemPathAnnotation: itemPath,
op.NameAnnotation: name,
op.ItemReferenceAnnotation: ItemReference,
op.NameAnnotation: name,
},
},
},
Expand Down Expand Up @@ -268,8 +268,8 @@ var tests = []testReconcileItem{
Name: name,
Namespace: namespace,
Annotations: map[string]string{
op.ItemPathAnnotation: itemPath,
op.NameAnnotation: name,
op.ItemReferenceAnnotation: ItemReference,
op.NameAnnotation: name,
},
},
},
Expand Down Expand Up @@ -310,8 +310,8 @@ var tests = []testReconcileItem{
Name: name,
Namespace: namespace,
Annotations: map[string]string{
op.ItemPathAnnotation: itemPath,
op.NameAnnotation: name,
op.ItemReferenceAnnotation: ItemReference,
op.NameAnnotation: name,
},
},
},
Expand Down Expand Up @@ -352,8 +352,8 @@ var tests = []testReconcileItem{
Name: name,
Namespace: namespace,
Annotations: map[string]string{
op.ItemPathAnnotation: itemPath,
op.NameAnnotation: name,
op.ItemReferenceAnnotation: ItemReference,
op.NameAnnotation: name,
},
},
},
Expand Down
3 changes: 2 additions & 1 deletion pkg/controller/onepassworditem/onepassworditem_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package onepassworditem
import (
"context"
"fmt"

onepasswordv1 "github.com/1Password/onepassword-operator/pkg/apis/onepassword/v1"
kubeSecrets "github.com/1Password/onepassword-operator/pkg/kubernetessecrets"
"github.com/1Password/onepassword-operator/pkg/onepassword"
Expand Down Expand Up @@ -145,7 +146,7 @@ func (r *ReconcileOnePasswordItem) HandleOnePasswordItem(resource *onepasswordv1
secretName := resource.GetName()
autoRestart := resource.Annotations[op.RestartDeploymentsAnnotation]

item, err := onepassword.GetOnePasswordItemByPath(r.opConnectClient, resource.Spec.ItemPath)
item, err := onepassword.GetOnePasswordItemByReference(r.opConnectClient, resource.Spec.ItemReference)
if err != nil {
return fmt.Errorf("Failed to retrieve item: %v", err)
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/controller/onepassworditem/onepassworditem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var (
"password": []byte(password),
"username": []byte(username),
}
itemPath = fmt.Sprintf("vaults/%v/items/%v", vaultId, itemId)
itemReference = fmt.Sprintf("op://%v/%v", vaultId, itemId)
)

var (
Expand All @@ -79,7 +79,7 @@ var tests = []testReconcileItem{
},
},
Spec: onepasswordv1.OnePasswordItemSpec{
ItemPath: itemPath,
ItemReference: itemReference,
},
},
existingSecret: &corev1.Secret{
Expand Down Expand Up @@ -111,7 +111,7 @@ var tests = []testReconcileItem{
Namespace: namespace,
},
Spec: onepasswordv1.OnePasswordItemSpec{
ItemPath: itemPath,
ItemReference: itemReference,
},
},
existingSecret: &corev1.Secret{
Expand Down Expand Up @@ -152,7 +152,7 @@ var tests = []testReconcileItem{
Namespace: namespace,
},
Spec: onepasswordv1.OnePasswordItemSpec{
ItemPath: itemPath,
ItemReference: itemReference,
},
},
existingSecret: &corev1.Secret{
Expand Down Expand Up @@ -193,7 +193,7 @@ var tests = []testReconcileItem{
Namespace: namespace,
},
Spec: onepasswordv1.OnePasswordItemSpec{
ItemPath: itemPath,
ItemReference: itemReference,
},
},
existingSecret: nil,
Expand Down Expand Up @@ -225,7 +225,7 @@ var tests = []testReconcileItem{
Namespace: namespace,
},
Spec: onepasswordv1.OnePasswordItemSpec{
ItemPath: itemPath,
ItemReference: itemReference,
},
},
existingSecret: nil,
Expand Down Expand Up @@ -257,7 +257,7 @@ var tests = []testReconcileItem{
Namespace: namespace,
},
Spec: onepasswordv1.OnePasswordItemSpec{
ItemPath: itemPath,
ItemReference: itemReference,
},
},
existingSecret: nil,
Expand Down
6 changes: 3 additions & 3 deletions pkg/kubernetessecrets/kubernetes_secrets_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const OnepasswordPrefix = "operator.1password.io"
const NameAnnotation = OnepasswordPrefix + "/item-name"
const VersionAnnotation = OnepasswordPrefix + "/item-version"
const restartAnnotation = OnepasswordPrefix + "/last-restarted"
const ItemPathAnnotation = OnepasswordPrefix + "/item-path"
const ItemReferenceAnnotation = OnepasswordPrefix + "/item-reference"
const RestartDeploymentsAnnotation = OnepasswordPrefix + "/auto-restart"

var log = logf.Log
Expand All @@ -32,8 +32,8 @@ func CreateKubernetesSecretFromItem(kubeClient kubernetesClient.Client, secretNa

itemVersion := fmt.Sprint(item.Version)
annotations := map[string]string{
VersionAnnotation: itemVersion,
ItemPathAnnotation: fmt.Sprintf("vaults/%v/items/%v", item.Vault.ID, item.ID),
VersionAnnotation: itemVersion,
ItemReferenceAnnotation: fmt.Sprintf("op://%v/%v", item.Vault.ID, item.ID),
}
if autoRestart != "" {
_, err := utils.StringToBool(autoRestart)
Expand Down
21 changes: 5 additions & 16 deletions pkg/kubernetessecrets/kubernetes_secrets_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package kubernetessecrets
import (
"context"
"fmt"
kubeValidate "k8s.io/apimachinery/pkg/util/validation"
"strings"
"testing"

kubeValidate "k8s.io/apimachinery/pkg/util/validation"

"github.com/1Password/connect-sdk-go/onepassword"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -42,7 +43,7 @@ func TestCreateKubernetesSecretFromOnePasswordItem(t *testing.T) {
t.Errorf("Secret was not created: %v", err)
}
compareFields(item.Fields, createdSecret.Data, t)
compareAnnotationsToItem(createdSecret.Annotations, item, t)
compareAnnotationsToItem(item.Vault.ID, item.ID, createdSecret.Annotations, item, t)
}

func TestUpdateKubernetesSecretFromOnePasswordItem(t *testing.T) {
Expand Down Expand Up @@ -78,7 +79,7 @@ func TestUpdateKubernetesSecretFromOnePasswordItem(t *testing.T) {
t.Errorf("Secret was not found: %v", err)
}
compareFields(newItem.Fields, updatedSecret.Data, t)
compareAnnotationsToItem(updatedSecret.Annotations, newItem, t)
compareAnnotationsToItem(newItem.Vault.ID, newItem.ID, updatedSecret.Annotations, newItem, t)
}
func TestBuildKubernetesSecretData(t *testing.T) {
fields := generateFields(5)
Expand Down Expand Up @@ -152,11 +153,7 @@ func TestBuildKubernetesSecretFixesInvalidLabels(t *testing.T) {
}
}

func compareAnnotationsToItem(annotations map[string]string, item onepassword.Item, t *testing.T) {
actualVaultId, actualItemId, err := ParseVaultIdAndItemIdFromPath(annotations[ItemPathAnnotation])
if err != nil {
t.Errorf("Was unable to parse Item Path")
}
func compareAnnotationsToItem(actualVaultId, actualItemId string, annotations map[string]string, item onepassword.Item, t *testing.T) {
if actualVaultId != item.Vault.ID {
t.Errorf("Expected annotation vault id to be %v but was %v", item.Vault.ID, actualVaultId)
}
Expand Down Expand Up @@ -196,14 +193,6 @@ func generateFields(numToGenerate int) []*onepassword.ItemField {
return fields
}

func ParseVaultIdAndItemIdFromPath(path string) (string, string, error) {
splitPath := strings.Split(path, "/")
if len(splitPath) == 4 && splitPath[0] == "vaults" && splitPath[2] == "items" {
return splitPath[1], splitPath[3], nil
}
return "", "", fmt.Errorf("%q is not an acceptable path for One Password item. Must be of the format: `vaults/{vault_id}/items/{item_id}`", path)
}

func validLabel(v string) bool {
if err := kubeValidate.IsDNS1123Subdomain(v); len(err) > 0 {
return false
Expand Down
2 changes: 1 addition & 1 deletion pkg/onepassword/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

const (
OnepasswordPrefix = "operator.1password.io"
ItemPathAnnotation = OnepasswordPrefix + "/item-path"
ItemReferenceAnnotation = OnepasswordPrefix + "/item-reference"
NameAnnotation = OnepasswordPrefix + "/item-name"
VersionAnnotation = OnepasswordPrefix + "/item-version"
RestartAnnotation = OnepasswordPrefix + "/last-restarted"
Expand Down
6 changes: 3 additions & 3 deletions pkg/onepassword/annotations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestFilterAnnotations(t *testing.T) {
if len(filteredAnnotations) != 2 {
t.Errorf("Unexpected number of filtered annotations returned. Expected 2, got %v", len(filteredAnnotations))
}
_, found := filteredAnnotations[ItemPathAnnotation]
_, found := filteredAnnotations[ItemReferenceAnnotation]
if !found {
t.Errorf("One Password Annotation was filtered when it should not have been")
}
Expand Down Expand Up @@ -87,7 +87,7 @@ func TestGetNoAnnotationsForDeployment(t *testing.T) {

func getValidAnnotations() map[string]string {
return map[string]string{
ItemPathAnnotation: "vaults/b3e4c7fc-8bf7-4c22-b8bb-147539f10e4f/items/b3e4c7fc-8bf7-4c22-b8bb-147539f10e4f",
NameAnnotation: "secretName",
ItemReferenceAnnotation: "op://b3e4c7fc-8bf7-4c22-b8bb-147539f10e4f/b3e4c7fc-8bf7-4c22-b8bb-147539f10e4f",
NameAnnotation: "secretName",
}
}
Loading

0 comments on commit bb228ca

Please sign in to comment.