|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "0fb7f3db", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "*Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved* \n", |
| 9 | + "\n", |
| 10 | + "*SPDX-License-Identifier: MIT-0* " |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "markdown", |
| 15 | + "id": "b78e5991", |
| 16 | + "metadata": {}, |
| 17 | + "source": [ |
| 18 | + "---" |
| 19 | + ] |
| 20 | + }, |
| 21 | + { |
| 22 | + "cell_type": "markdown", |
| 23 | + "id": "81e637d6", |
| 24 | + "metadata": {}, |
| 25 | + "source": [ |
| 26 | + "## Connecting to Amazon Keyspaces using server-side credentials \n", |
| 27 | + "\n", |
| 28 | + "This code shows how to connect to Amazon Keyspaces from SageMaker using an [service-specific credentials](https://docs.aws.amazon.com/keyspaces/latest/devguide/programmatic.credentials.html) for an existing AWS Identity and Access Management (IAM) user.\n" |
| 29 | + ] |
| 30 | + }, |
| 31 | + { |
| 32 | + "cell_type": "markdown", |
| 33 | + "id": "8a1a923c", |
| 34 | + "metadata": {}, |
| 35 | + "source": [ |
| 36 | + "Before we start we need to generate the Keyspaces credential and use SecretManager to securly store credentials. \n", |
| 37 | + "\n", |
| 38 | + "\n", |
| 39 | + "1. Generate [Keyspaces Service-Specific Credentials](https://docs.aws.amazon.com/keyspaces/latest/devguide/programmatic.credentials.html)\n", |
| 40 | + "\n", |
| 41 | + "\n", |
| 42 | + "The following is an example of a service-specific credential .\n", |
| 43 | + "\n", |
| 44 | + "```\n", |
| 45 | + "\"ServiceSpecificCredential\": {\n", |
| 46 | + " \"CreateDate\": \"2019-10-09T16:12:04Z\",\n", |
| 47 | + " \"ServiceName\": \"cassandra.amazonaws.com\",\n", |
| 48 | + " \"ServiceUserName\": \"keyspace-user1-at-11122223333\",\n", |
| 49 | + " \"ServicePassword\": \"wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY\",\n", |
| 50 | + " \"ServiceSpecificCredentialId\": \"ACCAYFI33SINPGJEBYESF\",\n", |
| 51 | + " \"UserName\": \" keyspace-user1\",\n", |
| 52 | + " \"Status\": \"Active\"\n", |
| 53 | + " }\n", |
| 54 | + "}\n", |
| 55 | + "```\n", |
| 56 | + "\n", |
| 57 | + "2. Store ServiceUserName and ServicePassword in the SecretManager. As a best practice we don't want to store credential in SageMaker Notebooks in plain text. \n", |
| 58 | + "\n", |
| 59 | + " In this example I'm using \n", |
| 60 | + "*Keyspaces_Server_Generated_credential* as a Secret name and _keyspaces_generated_id_ and _keyspaces_generated_pw_ fields to store Keyspaces ID and password. \n" |
| 61 | + ] |
| 62 | + }, |
| 63 | + { |
| 64 | + "cell_type": "code", |
| 65 | + "execution_count": null, |
| 66 | + "id": "a91e2ab0", |
| 67 | + "metadata": {}, |
| 68 | + "outputs": [], |
| 69 | + "source": [ |
| 70 | + "#Import \n", |
| 71 | + "from cassandra.cluster import Cluster\n", |
| 72 | + "from ssl import SSLContext, PROTOCOL_TLSv1_2 , CERT_REQUIRED\n", |
| 73 | + "from cassandra.auth import PlainTextAuthProvider\n", |
| 74 | + "\n", |
| 75 | + "ssl_context = SSLContext(PROTOCOL_TLSv1_2 )" |
| 76 | + ] |
| 77 | + }, |
| 78 | + { |
| 79 | + "cell_type": "code", |
| 80 | + "execution_count": null, |
| 81 | + "id": "68e63b3f", |
| 82 | + "metadata": {}, |
| 83 | + "outputs": [], |
| 84 | + "source": [ |
| 85 | + "#Download certificate \n", |
| 86 | + "!curl https://certs.secureserver.net/repository/sf-class2-root.crt -O " |
| 87 | + ] |
| 88 | + }, |
| 89 | + { |
| 90 | + "cell_type": "code", |
| 91 | + "execution_count": null, |
| 92 | + "id": "a714c7dc", |
| 93 | + "metadata": {}, |
| 94 | + "outputs": [], |
| 95 | + "source": [ |
| 96 | + "# Load certificate \n", |
| 97 | + "ssl_context.load_verify_locations('sf-class2-root.crt') " |
| 98 | + ] |
| 99 | + }, |
| 100 | + { |
| 101 | + "cell_type": "code", |
| 102 | + "execution_count": null, |
| 103 | + "id": "265d59f0", |
| 104 | + "metadata": {}, |
| 105 | + "outputs": [], |
| 106 | + "source": [ |
| 107 | + "#Getting Credential from Secert Manager \n", |
| 108 | + "#Need to have a SecretsManagerReadWrite Policy\n", |
| 109 | + "\n", |
| 110 | + "import boto3\n", |
| 111 | + "import base64\n", |
| 112 | + "from botocore.exceptions import ClientError\n", |
| 113 | + "import json\n", |
| 114 | + "\n", |
| 115 | + "secret_name = \"Keyspaces_Server_Generated_credential\"\n", |
| 116 | + "region_name = \"us-west-1\"\n", |
| 117 | + "\n", |
| 118 | + "# Create a Secrets Manager client\n", |
| 119 | + "session = boto3.session.Session()\n", |
| 120 | + "client = session.client(\n", |
| 121 | + " service_name='secretsmanager',\n", |
| 122 | + " region_name=region_name\n", |
| 123 | + ")\n", |
| 124 | + "\n", |
| 125 | + "\n", |
| 126 | + "get_secret_value_response = client.get_secret_value(\n", |
| 127 | + " SecretId=secret_name\n", |
| 128 | + ")\n", |
| 129 | + "\n", |
| 130 | + "# Decrypts secret using the associated KMS CMK.\n", |
| 131 | + "# Depending on whether the secret is a string or binary, one of these fields will be populated.\n", |
| 132 | + "if 'SecretString' in get_secret_value_response:\n", |
| 133 | + " secret = json.loads(get_secret_value_response['SecretString'])\n", |
| 134 | + " ServiceUserName = secret['keyspaces_generated_id']\n", |
| 135 | + " ServicePassword = secret['keyspaces_generated_pw']\n", |
| 136 | + " print(\"id:\",ServiceUserName, \", pw:\",ServicePassword)\n", |
| 137 | + "else:\n", |
| 138 | + " decoded_binary_secret = base64.b64decode(get_secret_value_response['SecretBinary'])\n", |
| 139 | + " \n", |
| 140 | + " " |
| 141 | + ] |
| 142 | + }, |
| 143 | + { |
| 144 | + "cell_type": "code", |
| 145 | + "execution_count": null, |
| 146 | + "id": "2a98e386", |
| 147 | + "metadata": {}, |
| 148 | + "outputs": [], |
| 149 | + "source": [ |
| 150 | + "auth_provider = PlainTextAuthProvider(username=ServiceUserName, password=ServicePassword)" |
| 151 | + ] |
| 152 | + }, |
| 153 | + { |
| 154 | + "cell_type": "code", |
| 155 | + "execution_count": null, |
| 156 | + "id": "bad5758b", |
| 157 | + "metadata": {}, |
| 158 | + "outputs": [], |
| 159 | + "source": [ |
| 160 | + "cluster = Cluster(['cassandra.us-west-1.amazonaws.com'], ssl_context=ssl_context, auth_provider=auth_provider, port=9142)\n" |
| 161 | + ] |
| 162 | + }, |
| 163 | + { |
| 164 | + "cell_type": "code", |
| 165 | + "execution_count": null, |
| 166 | + "id": "6b22f21d", |
| 167 | + "metadata": {}, |
| 168 | + "outputs": [], |
| 169 | + "source": [ |
| 170 | + "session = cluster.connect()\n", |
| 171 | + "cql = \"select * from system_schema.keyspaces \"\n", |
| 172 | + "r = session.execute(cql)\n", |
| 173 | + "print(r.current_rows)" |
| 174 | + ] |
| 175 | + }, |
| 176 | + { |
| 177 | + "cell_type": "code", |
| 178 | + "execution_count": null, |
| 179 | + "id": "eb124f18", |
| 180 | + "metadata": {}, |
| 181 | + "outputs": [], |
| 182 | + "source": [ |
| 183 | + "# Load rows to Panda DataFrame \n", |
| 184 | + "from pandas import DataFrame\n", |
| 185 | + "\n", |
| 186 | + "df = DataFrame(r)\n", |
| 187 | + "print(df)" |
| 188 | + ] |
| 189 | + }, |
| 190 | + { |
| 191 | + "cell_type": "code", |
| 192 | + "execution_count": null, |
| 193 | + "id": "3c9b9169", |
| 194 | + "metadata": {}, |
| 195 | + "outputs": [], |
| 196 | + "source": [] |
| 197 | + } |
| 198 | + ], |
| 199 | + "metadata": { |
| 200 | + "kernelspec": { |
| 201 | + "display_name": "conda_python3", |
| 202 | + "language": "python", |
| 203 | + "name": "conda_python3" |
| 204 | + }, |
| 205 | + "language_info": { |
| 206 | + "codemirror_mode": { |
| 207 | + "name": "ipython", |
| 208 | + "version": 3 |
| 209 | + }, |
| 210 | + "file_extension": ".py", |
| 211 | + "mimetype": "text/x-python", |
| 212 | + "name": "python", |
| 213 | + "nbconvert_exporter": "python", |
| 214 | + "pygments_lexer": "ipython3", |
| 215 | + "version": "3.6.13" |
| 216 | + } |
| 217 | + }, |
| 218 | + "nbformat": 4, |
| 219 | + "nbformat_minor": 5 |
| 220 | +} |
0 commit comments