Skip to content

feat(transform): Add hash string conversion#3498

Merged
turkenh merged 2 commits intocrossplane:masterfrom
MisterMX:feat/transform-hash
Jan 24, 2023
Merged

feat(transform): Add hash string conversion#3498
turkenh merged 2 commits intocrossplane:masterfrom
MisterMX:feat/transform-hash

Conversation

@MisterMX
Copy link
Copy Markdown
Contributor

@MisterMX MisterMX commented Dec 2, 2022

Description of your changes

This adds three new string conversion function to patch transforms, ToSha1, ToSha256, ToSha512, that generate a hash from the JSON representation of the input value.

I have:

  • Read and followed Crossplane's contribution process.
  • Run make reviewable to ensure this PR is ready for review.
  • Added backport release-x.y labels to auto-backport this PR if necessary.

How has this code been tested

composition.yaml

---
apiVersion: apiextensions.crossplane.io/v1
kind: Composition
metadata:
  name: test-string-tojson
spec:
  compositeTypeRef:
    apiVersion: example.org/v1alpha1
    kind: XSecret
  resources:
    - name: secret
      base:
        apiVersion: secretsmanager.aws.crossplane.io/v1beta1
        kind: Secret
        metadata:
          name: # patched
          annotations:
            hash: # patched
        spec:
          forProvider:
            region: eu-central-1
            stringSecretRef:
              name: # patched
              namespace: # patched
              key: # patched
          providerConfigRef:
            name: example
      patches:
        # Policy
        - fromFieldPath: "spec.parameters"
          toFieldPath: "metadata.annotations[hash]"
          transforms:
            - type: string
              string:
                type: Convert
                convert: ToSha1
        # SecretRef
        - fromFieldPath: "spec.parameters.secretRef.name"
          toFieldPath: "spec.forProvider.stringSecretRef.name"
        - fromFieldPath: "spec.parameters.secretRef.key"
          toFieldPath: "spec.forProvider.stringSecretRef.key"
        - fromFieldPath: "metadata.labels[crossplane.io/claim-namespace]"
          toFieldPath: "spec.forProvider.stringSecretRef.namespace"

xrd.yaml

---
apiVersion: apiextensions.crossplane.io/v1
kind: CompositeResourceDefinition
metadata:
  name: xsecrets.example.org
spec:
  group: example.org
  claimNames:
    kind: Secret
    plural: secrets
  names:
    kind: XSecret
    plural: xsecrets
  versions:
    - name: v1alpha1
      served: true
      referenceable: true
      schema:
        openAPIV3Schema:
          type: object
          properties:
            spec:
              type: object
              properties:
                parameters:
                  type: object
                  properties:
                    secretRef:
                      type: object
                      description: The Kubernetes Secret whose data will be sent as string to AWS.
                        The secret needs to exist in the same namespace as this claim.
                      properties:
                        key:
                          type: string
                          description: Optional key whose value should be uploaded. If not set, the
                            whole map in the Secret data will be used.
                        name:
                          type: string
                          description: Name of the Kubernetes Secret resource in the same namespace
                            as this claim.
                      required:
                        - name
              required:
                - parameters

xrc.yaml

---
apiVersion: v1
kind: Secret
metadata:
  name: example-secret
  namespace: default
type: Opaque
stringData:
  password: passw0rd
---
apiVersion: example.org/v1alpha1
kind: Secret
metadata:
  name: example-secret
  namespace: default
spec:
  parameters:
    secretRef:
      name: example-secret

@MisterMX MisterMX requested review from a team and muvaf as code owners December 2, 2022 11:03
@MisterMX MisterMX requested review from ezgidemirel and removed request for a team December 2, 2022 11:03
@MisterMX MisterMX force-pushed the feat/transform-hash branch from 63d5fe6 to e9144d1 Compare December 2, 2022 11:09
@MisterMX MisterMX force-pushed the feat/transform-hash branch 2 times, most recently from fb18a65 to e8f9e0d Compare December 13, 2022 09:34
@MisterMX MisterMX force-pushed the feat/transform-hash branch 3 times, most recently from 3abecc0 to 29eb1f1 Compare January 18, 2023 12:57
@MisterMX MisterMX requested review from turkenh and ytsarev January 18, 2023 15:07
@jbw976 jbw976 added this to the v1.11 milestone Jan 18, 2023
Comment on lines +280 to +281
func stringGenerateHash[THash any](input any, hashFunc func([]byte) THash) (THash, error) {
inputJSON, err := json.Marshal(input)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the first usage of generics in Crossplane 🤔

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its a feature of the newer Go version. Is that a problem? Its only a very small function.

Comment thread docs/reference/composition.md Outdated
Copy link
Copy Markdown
Member

@turkenh turkenh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @MisterMX!

Looks like we need a rebase here too.

Signed-off-by: Maximilian Blatt <maximilian.blatt-extern@deutschebahn.com>
(external expert on behalf of DB Netz AG)
Signed-off-by: Maximilian Blatt <maximilian.blatt-extern@deutschebahn.com>
(external expert on behalf of DB Netz AG)
@MisterMX MisterMX force-pushed the feat/transform-hash branch from b013a42 to a0dc38e Compare January 24, 2023 08:41
@turkenh turkenh merged commit f347c2e into crossplane:master Jan 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants