TokenResult GetTokenForIAM()

in connection.c [1259:1317]


TokenResult GetTokenForIAM(ConnInfo* ci, BOOL useCache) {
	MYLOG(MIN_LOG_LEVEL, "entering...\n");

	if (!ci) {
		MYLOG(MIN_LOG_LEVEL, "Null ConnInfo pointer\n");
		return TR_FAILURE;
	}

	int port = pg_atoi(ci->port);
	if (port < 1) {
		port = 5432; // set to default port.
	}

	char *server = ci->iam_host && *ci->iam_host != 0 ? ci->iam_host : ci->server;

	MYLOG(MIN_LOG_LEVEL, "auth type is %s\n", ci->authtype);
	MYLOG(MIN_LOG_LEVEL, "server is %s\n", ci->server);
	MYLOG(MIN_LOG_LEVEL, "iam host is %s\n", ci->iam_host);
	MYLOG(MIN_LOG_LEVEL, "region is %s\n", ci->region);
	MYLOG(MIN_LOG_LEVEL, "port is %d\n", port);
	MYLOG(MIN_LOG_LEVEL, "username is %s\n", ci->username);
	MYLOG(MIN_LOG_LEVEL, "useCache is %d\n", useCache);

	char* token = (char*) malloc(MAX_TOKEN_SIZE * sizeof(char));
	// Fill in password to avoid crashing on token failures
	STRN_TO_NAME(ci->password, token, 0);
	FederatedAuthType authType = GetFedAuthTypeEnum(ci->authtype);

	if (useCache) {
		MYLOG(MIN_LOG_LEVEL, "Trying Cache\n");
		if (!GetCachedToken(token, MAX_TOKEN_SIZE, server, ci->region, ci->port, ci->username)) {
			MYLOG(MIN_LOG_LEVEL, "Cache Miss\n");
			if (!GenerateConnectAuthToken(token, MAX_TOKEN_SIZE, server, ci->region, port, ci->username, authType, ci->federation_cfg)) {
				MYLOG(MIN_LOG_LEVEL, "Failed to generate a RDS connect auth token\n");
				free(token);
				return TR_FAILURE;
			}
			STRN_TO_NAME(ci->password, token, strlen(token));
			MYLOG(MIN_LOG_LEVEL, "generated token length is %zu\n", strlen(ci->password.name));
		}
		else {
			STRN_TO_NAME(ci->password, token, strlen(token));
			MYLOG(MIN_LOG_LEVEL, "cached token length is %zu\n", strlen(ci->password.name));
			free(token);
			return TR_CACHED_TOKEN;
		}
	}
	else {
		if (!GenerateConnectAuthToken(token, MAX_TOKEN_SIZE, server, ci->region, port, ci->username, authType, ci->federation_cfg)) {
			MYLOG(MIN_LOG_LEVEL, "Failed to generate a RDS connect auth token\n");
			free(token);
			return TR_FAILURE;
		}
		STRN_TO_NAME(ci->password, token, strlen(token));
		MYLOG(MIN_LOG_LEVEL, "generated token length is %zu\n", strlen(ci->password.name));
	}
	free(token);
	return TR_GENERATED_TOKEN;
}