|
100 | 100 | { |
101 | 101 | "cell_type": "markdown", |
102 | 102 | "source": [ |
103 | | - "## Connecting to a redis instance with static credential provider" |
| 103 | + "## Connecting to a redis instance with username and password credential provider" |
104 | 104 | ], |
105 | 105 | "metadata": {} |
106 | 106 | }, |
|
111 | 111 | "source": [ |
112 | 112 | "import redis\n", |
113 | 113 | "\n", |
114 | | - "creds_provider = redis.StaticCredentialProvider(\"username\", \"password\")\n", |
| 114 | + "creds_provider = redis.UsernamePasswordCredentialProvider(\"username\", \"password\")\n", |
115 | 115 | "user_connection = redis.Redis(host=\"localhost\", port=6379, credential_provider=creds_provider)\n", |
116 | 116 | "user_connection.ping()" |
117 | 117 | ], |
|
131 | 131 | "execution_count": null, |
132 | 132 | "outputs": [], |
133 | 133 | "source": [ |
| 134 | + "from typing import Tuple\n", |
134 | 135 | "import redis\n", |
135 | 136 | "\n", |
136 | 137 | "creds_map = {\"user_1\": \"pass_1\",\n", |
137 | 138 | " \"user_2\": \"pass_2\"}\n", |
138 | 139 | "\n", |
| 140 | + "class UserMapCredentialProvider(redis.CredentialProvider):\n", |
| 141 | + " def __init__(self, username: str):\n", |
| 142 | + " self.username = username\n", |
| 143 | + "\n", |
| 144 | + " def get_credentials(self) -> Tuple[str, str]:\n", |
| 145 | + " return self.username, creds_map.get(self.username)\n", |
| 146 | + "\n", |
139 | 147 | "# Create a default connection to set the ACL user\n", |
140 | 148 | "default_connection = redis.Redis(host=\"localhost\", port=6379)\n", |
141 | 149 | "default_connection.acl_setuser(\n", |
|
146 | 154 | " commands=[\"+ping\", \"+command\", \"+info\", \"+select\", \"+flushdb\"],\n", |
147 | 155 | ")\n", |
148 | 156 | "\n", |
149 | | - "def creds_provider(self):\n", |
150 | | - " return self.username, creds_map.get(self.username)\n", |
151 | | - "\n", |
152 | | - "# Create a CredentialProvider instance for user_1\n", |
153 | | - "creds_provider = redis.CredentialProvider(username=\"user_1\", supplier=creds_provider)\n", |
| 157 | + "# Create a UserMapCredentialProvider instance for user_1\n", |
| 158 | + "creds_provider = UserMapCredentialProvider(\"user_1\")\n", |
154 | 159 | "# Initiate user connection with the credential provider\n", |
155 | 160 | "user_connection = redis.Redis(host=\"localhost\", port=6379,\n", |
156 | 161 | " credential_provider=creds_provider)\n", |
|
172 | 177 | "execution_count": null, |
173 | 178 | "outputs": [], |
174 | 179 | "source": [ |
| 180 | + "from typing import Union\n", |
175 | 181 | "import redis\n", |
176 | 182 | "\n", |
177 | | - "def call_external_supplier():\n", |
178 | | - " # Call to an external credential supplier\n", |
179 | | - " raise NotImplementedError\n", |
| 183 | + "class InitCredsSetCredentialProvider(redis.CredentialProvider):\n", |
| 184 | + " def __init__(self, username, password):\n", |
| 185 | + " self.username = username\n", |
| 186 | + " self.password = password\n", |
| 187 | + " self.call_supplier = False\n", |
| 188 | + "\n", |
| 189 | + " def call_external_supplier(self) -> Union[Tuple[str], Tuple[str, str]]:\n", |
| 190 | + " # Call to an external credential supplier\n", |
| 191 | + " raise NotImplementedError\n", |
180 | 192 | "\n", |
181 | | - "def creds_supplier(self):\n", |
182 | | - " call_supplier = self.supplier_kwargs.get(\"call_supplier\", True)\n", |
183 | | - " if call_supplier:\n", |
184 | | - " return call_external_supplier()\n", |
185 | | - " # Use the init set only for the first time\n", |
186 | | - " self.kwargs.update({\"call_supplier\": True})\n", |
187 | | - " return self.username, self.password\n", |
| 193 | + " def get_credentials(self) -> Union[Tuple[str], Tuple[str, str]]:\n", |
| 194 | + " if self.call_supplier:\n", |
| 195 | + " return self.call_external_supplier()\n", |
| 196 | + " # Use the init set only for the first time\n", |
| 197 | + " self.call_supplier = True\n", |
| 198 | + " return self.username, self.password\n", |
188 | 199 | "\n", |
189 | | - "cred_provider = redis.CredentialProvider(username=\"init_user\",\n", |
190 | | - " password=\"init_pass\",\n", |
191 | | - " call_supplier=False,\n", |
192 | | - " supplier=creds_supplier)" |
| 200 | + "cred_provider = InitCredsSetCredentialProvider(username=\"init_user\", password=\"init_pass\")" |
193 | 201 | ], |
194 | 202 | "metadata": {} |
195 | 203 | } |
|
0 commit comments