postgres-operator icon indicating copy to clipboard operation
postgres-operator copied to clipboard

Create secrets in specific namespace

Open synchris opened this issue 2 years ago • 15 comments

A way to instruct the operator to create a secret in a specific namespace for the user and database mentioned in the CRD configuration. So will diminish the need to use SecretImport SecretExport for instance as it creates a lot of hustle. This will help to separate the PostgreSQL cluster to different namespaces from the running applications.

synchris avatar Nov 04 '21 20:11 synchris

This could be doable. It does mean that you would have to give the Operator privileges to interact with the application Namespaces. Would that be acceptable?

jkatz avatar Nov 05 '21 13:11 jkatz

Yes, the operator should have access to create secrets in those namespaces.

synchris avatar Nov 07 '21 12:11 synchris

Hello, is there any progress about this feature? I think this should be in the PostgresCluster.spec.users.secretNamespaces as []string and it defaulted to the namespace of cluster if not defined. It's very common that the container the user want to connect to is not in the namespace of the postgresCluster.

budimanjojo avatar Mar 08 '22 15:03 budimanjojo

Seems like a critical enhancement for a security conscious (any) deployment

B1ue-W01f avatar Jun 18 '22 10:06 B1ue-W01f

This is a critical feature for me as well. I'd like to switch to this operator but we use multiple apps running in different namespaces which makes it difficult to retrieve their passwords from a single namespace.

philmccOri avatar Jul 07 '22 08:07 philmccOri

+1 Requesting support for this feature

azharullah avatar Aug 15 '22 12:08 azharullah

Thanks for all of the feedback and input on this one. Just noting that a story has been added to the PGO backlog for consideration in a future release.

andrewlecuyer avatar Oct 11 '22 15:10 andrewlecuyer

+1 It's a critical feature for me.

Or, I think adding custom annotations to the secret is also an ok choice, so that we can use tools like kubernetes-reflector to mirror the secret to another namespace.

LiamSho avatar Oct 15 '22 10:10 LiamSho

(Adding the backlog label just to indicate that we have this feature in our backlog and are tracking it.)

benjaminjb avatar Oct 17 '22 15:10 benjaminjb

+1 this would be a great feature to have to avoid using third party tools for secrets reflection

kaizendae avatar Jan 12 '23 10:01 kaizendae

i think the simplest way to handle this is a . split in the users[].name

example:

users:
    - name: rhino
      databases:
        - zoo
    - name: default.blah
       databases:
       - zoo

this would create the zoo database and users rhino and blah deploying the user pass for rhino in the PGO deployed ns and the user blah secret to the default namespace

thoughts?

sgraham785 avatar Feb 14 '23 03:02 sgraham785

I was looking at what it would take to make this happen. So far what I can see is that one would need to add a new part to the spec: https://github.com/jaitaiwan/postgres-operator/blob/master/pkg/apis/postgres-operator.crunchydata.com/v1beta1/postgres_types.go#L41

Ensure that the reconcile function knows what to do with it: https://github.com/jaitaiwan/postgres-operator/blob/master/internal/controller/postgrescluster/postgres.go#L321

I don't see anywhere else at first glance that needs any changes apart from any tests or e2e that might need to be done

jaitaiwan avatar Jan 02 '24 16:01 jaitaiwan

I really like the behavior of cert-manager's Certificate resource, which allows you to specify labels and annotations for the secret: https://cert-manager.io/docs/usage/certificate/

This then works together with tools like Reflector such that the secret can be arbitrarily copied to other namespaces:

apiVersion: cert-manager.io/v1
kind: Certificate
...
spec:
  secretTemplate:
    annotations:
      reflector.v1.k8s.emberstack.com/reflection-allowed: "true"
      reflector.v1.k8s.emberstack.com/reflection-allowed-namespaces: ""
  ...

The added benefit is that annotations and labels can be added for other purposes as well. Also, nothing about the operator's permissions regarding other namespaces needs to change.

i think the simplest way to handle this is a . split in the users[].name

While this would work, I think it is not optimal, since the secret may be needed in multiple namespaces.

meyfa avatar Jan 02 '24 17:01 meyfa

I ended up using jsPolicy to inject Reflector reflector.v1.k8s.emberstack.com/reflection-allowed annotations into the secret created by Postgres Operator. In this case for the user keycloak with secret reflection allowed into the keycloak namespace:

type: Mutating
operations: ["CREATE"]
resources: ["secrets"]
scope: "Namespaced"
namespaceSelector:
  matchExpressions:
    - key: kubernetes.io/metadata.name
      operator: In
      values: ["postgres-operator"]
javascript: |
  if (!request.name.endsWith("-pguser-keycloak")) {
    exit();
  }

  const secret = request.object;
  const annotations = secret.metadata.annotations || {};

  const hasAnnotation = !!annotations['reflector.v1.k8s.emberstack.com/reflection-allowed'];

  if (hasAnnotation) {
    exit();
  }

  const reflectorSettings = {
    "reflector.v1.k8s.emberstack.com/reflection-allowed": "true",
    "reflector.v1.k8s.emberstack.com/reflection-allowed-namespaces": "keycloak"
  };

  secret.metadata.annotations = {
    ...annotations,
    ...reflectorSettings
  };

  mutate(secret);

The mirror target then looks like e.g.

apiVersion: v1
kind: Secret
metadata:
 name: some-name
 namespace: keycloak
 annotations:
   reflector.v1.k8s.emberstack.com/reflects: "postgres-operator/hippo-pguser-keycloak"
data: {}

sunsided avatar Feb 14 '24 17:02 sunsided

i think the simplest way to handle this is a . split in the users[].name

example:

users:
    - name: rhino
      databases:
        - zoo
    - name: default.blah
       databases:
       - zoo

this would create the zoo database and users rhino and blah deploying the user pass for rhino in the PGO deployed ns and the user blah secret to the default namespace

thoughts?

I thinks it should be more explicit i.e namespace. default.blah seems too cryptic. But really hoping this can be released sooner.

itsSaad avatar Apr 05 '24 00:04 itsSaad