WriteUsersInPGAdmin uses exec and "python" to create users in pgAdmin and update their passwords when they already exist. A blank password for a user blocks that user from logging in to pgAdmin. The pgAdmin configuration database must exist before calling this.
( ctx context.Context, cluster *v1beta1.PostgresCluster, exec Executor, users []v1beta1.PostgresUserSpec, passwords map[string]string, )
| 26 | // blocks that user from logging in to pgAdmin. The pgAdmin configuration |
| 27 | // database must exist before calling this. |
| 28 | func WriteUsersInPGAdmin( |
| 29 | ctx context.Context, cluster *v1beta1.PostgresCluster, exec Executor, |
| 30 | users []v1beta1.PostgresUserSpec, passwords map[string]string, |
| 31 | ) error { |
| 32 | primary := naming.ClusterPrimaryService(cluster) |
| 33 | |
| 34 | args := []string{ |
| 35 | cluster.Name, |
| 36 | primary.Name + "." + primary.Namespace + ".svc", |
| 37 | fmt.Sprint(*cluster.Spec.Port), |
| 38 | } |
| 39 | script := strings.Join([]string{ |
| 40 | // Unpack arguments into an object. |
| 41 | // - https://docs.python.org/3/library/types.html#types.SimpleNamespace |
| 42 | ` |
| 43 | import sys |
| 44 | import types |
| 45 | |
| 46 | cluster = types.SimpleNamespace() |
| 47 | (cluster.name, cluster.hostname, cluster.port) = sys.argv[1:]`, |
| 48 | |
| 49 | // The location of pgAdmin files can vary by container image. Look for |
| 50 | // typical names in the module search path: the PyPI package is named |
| 51 | // "pgadmin4" while custom builds might use "pgadmin4-web". The pgAdmin |
| 52 | // packages expect to find themselves on the search path, so prepend |
| 53 | // that directory there (like pgAdmin does in its WSGI entrypoint). |
| 54 | // - https://pypi.org/project/pgadmin4/ |
| 55 | // - https://github.com/pgadmin-org/pgadmin4/blob/REL-4_30/web/pgAdmin4.wsgi#L18 |
| 56 | ` |
| 57 | import importlib.util |
| 58 | import os |
| 59 | import sys |
| 60 | |
| 61 | spec = importlib.util.find_spec('.pgadmin', ( |
| 62 | importlib.util.find_spec('pgadmin4') or |
| 63 | importlib.util.find_spec('pgadmin4-web') |
| 64 | ).name) |
| 65 | root = os.path.dirname(spec.submodule_search_locations[0]) |
| 66 | if sys.path[0] != root: |
| 67 | sys.path.insert(0, root)`, |
| 68 | |
| 69 | // Import pgAdmin modules now that they are on the search path. |
| 70 | // NOTE: When testing with the REPL, use the `__enter__` method to |
| 71 | // avoid one level of indentation. |
| 72 | // |
| 73 | // create_app().app_context().__enter__() |
| 74 | // |
| 75 | ` |
| 76 | import copy |
| 77 | import json |
| 78 | import sys |
| 79 | |
| 80 | from pgadmin import create_app |
| 81 | from pgadmin.model import db, Role, User, Server, ServerGroup |
| 82 | from pgadmin.utils.constants import INTERNAL |
| 83 | from pgadmin.utils.crypto import encrypt |
| 84 | |
| 85 | with create_app().app_context():`, |