// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 package com.amazon.redshift.plugin.tools; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.LaxRedirectStrategy; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import com.amazon.redshift.plugin.SamlCredentialsProvider; import com.amazonaws.SdkClientException; import com.amazonaws.util.IOUtils; import com.amazonaws.util.StringUtils; /** *
* This is the main plugin class. *
* * @author Dipankar Ghosal , Amazon Web Services, Inc. * */ public class SiteMinderCredentialProvider extends SamlCredentialsProvider { /** * Here we are defining a new connection property key called "sso_url". This property * will be specific to the SiteMinderSamlCredentialsProvider and will be used to provide SSO URL * information through the connection string. ** This means that a user wanting to use this credential provider may include the following in * the connection string: *
*
* jdbc:redshift:iam://[host]:[port]/[database]?sso_url=[value]
*
*
*
*/
private static final String SSO_URL = "sso_url";
protected String m_sso_url;
@Override
public void addParameter(String key, String value) {
super.addParameter(key, value);
if (SSO_URL.equalsIgnoreCase(key)) {
m_sso_url = value;
}
}
@Override
protected String getSamlAssertion() throws IOException {
if (StringUtils.isNullOrEmpty(m_sso_url)) {
throw new IOException("Missing required property: " + SSO_URL);
}
CloseableHttpClient httpClient = null;
try {
URLConnection con = new URL(m_sso_url).openConnection();
con.connect();
@SuppressWarnings("unused")
InputStream is = con.getInputStream();
String redirected_url = con.getURL().toString();
httpClient = buildHttpClient();
return handleSamlAssertion(httpClient, redirected_url);
} catch (Exception e) {
throw new SdkClientException("Failed to create SSLContext.", e);
} finally {
IOUtils.closeQuietly(httpClient, null);
}
}
/**
* Retrieves SAML assertion from Siteminder containing AWS roles.
*/
private String handleSamlAssertion(CloseableHttpClient httpClient, String redirected_url) throws IOException {
HttpPost httpost = new HttpPost(redirected_url);
String body = null;
ArrayList