Skip to content

Commit f8b0fb5

Browse files
amaksimoalemaksi
andauthored
Move auth token examples from starter-kit with snippet markers (#216)
- Move all inline code blocks from generate-authentication-token.md to samples repo - Examples organized by language (py, js, java, cpp, rs, rb, cs, go) Co-authored-by: Aleksandar Maksimovic <alemaksi@amazon.com>
1 parent 5c2e705 commit f8b0fb5

9 files changed

Lines changed: 220 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# CLI and CloudShell examples for generating Aurora DSQL authentication tokens
2+
3+
# --8<-- [start:cloudshell-admin-token]
4+
aws dsql generate-db-connect-admin-auth-token \
5+
--expires-in 3600 \
6+
--region us-east-1 \
7+
--hostname your_cluster_endpoint
8+
# --8<-- [end:cloudshell-admin-token]
9+
10+
# --8<-- [start:cloudshell-psql-connection]
11+
PGSSLMODE=require \
12+
psql --dbname postgres \
13+
--username admin \
14+
--host cluster_endpoint
15+
# --8<-- [end:cloudshell-psql-connection]
16+
17+
# --8<-- [start:cli-linux-macos]
18+
aws dsql generate-db-connect-admin-auth-token \
19+
--region region \
20+
--expires-in 3600 \
21+
--hostname your_cluster_endpoint
22+
# --8<-- [end:cli-linux-macos]
23+
24+
# --8<-- [start:cli-windows]
25+
aws dsql generate-db-connect-admin-auth-token ^
26+
--region=region ^
27+
--expires-in=3600 ^
28+
--hostname=your_cluster_endpoint
29+
# --8<-- [end:cli-windows]
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// C++ SDK examples for generating Aurora DSQL authentication tokens
2+
3+
// --8<-- [start:cpp-generate-token]
4+
#include <aws/core/Aws.h>
5+
#include <aws/dsql/DSQLClient.h>
6+
#include <iostream>
7+
8+
using namespace Aws;
9+
using namespace Aws::DSQL;
10+
11+
std::string generateToken(String yourClusterEndpoint, String region) {
12+
Aws::SDKOptions options;
13+
Aws::InitAPI(options);
14+
DSQLClientConfiguration clientConfig;
15+
clientConfig.region = region;
16+
DSQLClient client{clientConfig};
17+
std::string token = "";
18+
19+
// If you are not using the admin role to connect, use GenerateDBConnectAuthToken instead
20+
const auto presignedString = client.GenerateDBConnectAdminAuthToken(yourClusterEndpoint, region);
21+
if (presignedString.IsSuccess()) {
22+
token = presignedString.GetResult();
23+
} else {
24+
std::cerr << "Token generation failed." << std::endl;
25+
}
26+
27+
std::cout << token << std::endl;
28+
29+
Aws::ShutdownAPI(options);
30+
return token;
31+
}
32+
// --8<-- [end:cpp-generate-token]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// .NET SDK examples for generating Aurora DSQL authentication tokens
2+
3+
// --8<-- [start:dotnet-generate-token]
4+
using Amazon;
5+
using Amazon.DSQL.Util;
6+
using Amazon.Runtime;
7+
8+
var yourClusterEndpoint = "insert-dsql-cluster-endpoint";
9+
10+
AWSCredentials credentials = FallbackCredentialsFactory.GetCredentials();
11+
12+
var token = DSQLAuthTokenGenerator.GenerateDbConnectAdminAuthToken(credentials, RegionEndpoint.USEast1, yourClusterEndpoint);
13+
14+
Console.WriteLine(token);
15+
// --8<-- [end:dotnet-generate-token]
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Go SDK examples for generating Aurora DSQL authentication tokens
2+
3+
// --8<-- [start:go-generate-token]
4+
func GenerateDbConnectAdminAuthToken(yourClusterEndpoint string, region string, action string) (string, error) {
5+
// Fetch credentials
6+
sess, err := session.NewSession()
7+
if err != nil {
8+
return "", err
9+
}
10+
11+
creds, err := sess.Config.Credentials.Get()
12+
if err != nil {
13+
return "", err
14+
}
15+
staticCredentials := credentials.NewStaticCredentials(
16+
creds.AccessKeyID,
17+
creds.SecretAccessKey,
18+
creds.SessionToken,
19+
)
20+
21+
// The scheme is arbitrary and is only needed because validation of the URL requires one.
22+
endpoint := "https://" + yourClusterEndpoint
23+
req, err := http.NewRequest("GET", endpoint, nil)
24+
if err != nil {
25+
return "", err
26+
}
27+
values := req.URL.Query()
28+
values.Set("Action", action)
29+
req.URL.RawQuery = values.Encode()
30+
31+
signer := v4.Signer{
32+
Credentials: staticCredentials,
33+
}
34+
_, err = signer.Presign(req, nil, "dsql", region, 15*time.Minute, time.Now())
35+
if err != nil {
36+
return "", err
37+
}
38+
39+
url := req.URL.String()[len("https://"):]
40+
41+
return url, nil
42+
}
43+
// --8<-- [end:go-generate-token]
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Java SDK examples for generating Aurora DSQL authentication tokens
2+
3+
// --8<-- [start:java-generate-token]
4+
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
5+
import software.amazon.awssdk.services.dsql.DsqlUtilities;
6+
import software.amazon.awssdk.regions.Region;
7+
8+
public class GenerateAuthToken {
9+
public static String generateToken(String yourClusterEndpoint, Region region) {
10+
DsqlUtilities utilities = DsqlUtilities.builder()
11+
.region(region)
12+
.credentialsProvider(DefaultCredentialsProvider.create())
13+
.build();
14+
15+
// Use `generateDbConnectAuthToken` if you are _not_ logging in as `admin` user
16+
String token = utilities.generateDbConnectAdminAuthToken(builder -> {
17+
builder.hostname(yourClusterEndpoint)
18+
.region(region);
19+
});
20+
21+
System.out.println(token);
22+
return token;
23+
}
24+
}
25+
// --8<-- [end:java-generate-token]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// JavaScript SDK examples for generating Aurora DSQL authentication tokens
2+
3+
// --8<-- [start:javascript-generate-token]
4+
import { DsqlSigner } from "@aws-sdk/dsql-signer";
5+
6+
async function generateToken(yourClusterEndpoint, region) {
7+
const signer = new DsqlSigner({
8+
hostname: yourClusterEndpoint,
9+
region,
10+
});
11+
try {
12+
// Use `getDbConnectAuthToken` if you are _not_ logging in as the `admin` user
13+
const token = await signer.getDbConnectAdminAuthToken();
14+
console.log(token);
15+
return token;
16+
} catch (error) {
17+
console.error("Failed to generate token: ", error);
18+
throw error;
19+
}
20+
}
21+
// --8<-- [end:javascript-generate-token]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Python SDK examples for generating Aurora DSQL authentication tokens
2+
3+
# --8<-- [start:python-generate-token]
4+
def generate_token(your_cluster_endpoint, region):
5+
client = boto3.client("dsql", region_name=region)
6+
# use `generate_db_connect_auth_token` instead if you are not connecting as admin.
7+
token = client.generate_db_connect_admin_auth_token(your_cluster_endpoint, region)
8+
print(token)
9+
return token
10+
# --8<-- [end:python-generate-token]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Ruby SDK examples for generating Aurora DSQL authentication tokens
2+
3+
# --8<-- [start:ruby-generate-token]
4+
require 'aws-sdk-dsql'
5+
6+
def generate_token(your_cluster_endpoint, region)
7+
credentials = Aws::SharedCredentials.new()
8+
9+
begin
10+
token_generator = Aws::DSQL::AuthTokenGenerator.new({
11+
:credentials => credentials
12+
})
13+
14+
# if you're not using admin role, use generate_db_connect_auth_token instead
15+
token = token_generator.generate_db_connect_admin_auth_token({
16+
:endpoint => your_cluster_endpoint,
17+
:region => region
18+
})
19+
rescue => error
20+
puts error.full_message
21+
end
22+
end
23+
# --8<-- [end:ruby-generate-token]
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Rust SDK examples for generating Aurora DSQL authentication tokens
2+
3+
// --8<-- [start:rust-generate-token]
4+
use aws_config::{BehaviorVersion, Region};
5+
use aws_sdk_dsql::auth_token::{AuthTokenGenerator, Config};
6+
7+
async fn generate_token(your_cluster_endpoint: String, region: String) -> String {
8+
let sdk_config = aws_config::load_defaults(BehaviorVersion::latest()).await;
9+
let signer = AuthTokenGenerator::new(
10+
Config::builder()
11+
.hostname(&your_cluster_endpoint)
12+
.region(Region::new(region))
13+
.build()
14+
.unwrap(),
15+
);
16+
17+
// Use `db_connect_auth_token` if you are _not_ logging in as `admin` user
18+
let token = signer.db_connect_admin_auth_token(&sdk_config).await.unwrap();
19+
println!("{}", token);
20+
token.to_string()
21+
}
22+
// --8<-- [end:rust-generate-token]

0 commit comments

Comments
 (0)