MCPcopy Create free account
hub / github.com/CrunchyData/postgres-operator / WriteUsersInPGAdmin

Function WriteUsersInPGAdmin

internal/pgadmin/users.go:28–258  ·  view source on GitHub ↗

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,
)

Source from the content-addressed store, hash-verified

26// blocks that user from logging in to pgAdmin. The pgAdmin configuration
27// database must exist before calling this.
28func 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 `
43import sys
44import types
45
46cluster = 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 `
57import importlib.util
58import os
59import sys
60
61spec = importlib.util.find_spec('.pgadmin', (
62 importlib.util.find_spec('pgadmin4') or
63 importlib.util.find_spec('pgadmin4-web')
64).name)
65root = os.path.dirname(spec.submodule_search_locations[0])
66if 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 `
76import copy
77import json
78import sys
79
80from pgadmin import create_app
81from pgadmin.model import db, Role, User, Server, ServerGroup
82from pgadmin.utils.constants import INTERNAL
83from pgadmin.utils.crypto import encrypt
84
85with create_app().app_context():`,

Callers 2

reconcilePGAdminUsersMethod · 0.92
TestWriteUsersInPGAdminFunction · 0.85

Calls 4

ClusterPrimaryServiceFunction · 0.92
FromContextFunction · 0.92
InfoMethod · 0.80
StringMethod · 0.45

Tested by 1

TestWriteUsersInPGAdminFunction · 0.68