Skip to content

Commit b3e9d05

Browse files
committed
Pass AWS credentials via structure pointer
- Setting env variable is not an elegant solution to pass credentials - Pass these in config structure instead
1 parent 987d770 commit b3e9d05

File tree

3 files changed

+107
-37
lines changed

3 files changed

+107
-37
lines changed

esp_port/examples/app_common/include/sample_config.h

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,29 @@ extern "C" {
6969
#define MASTER_DATA_CHANNEL_MESSAGE "This message is from the KVS Master"
7070
#define VIEWER_DATA_CHANNEL_MESSAGE "This message is from the KVS Viewer"
7171

72+
/**
73+
* @brief Structure to hold AWS credentials and related options
74+
*/
75+
typedef struct {
76+
// IoT Core credentials
77+
BOOL enableIotCredentials;
78+
PCHAR iotCoreCredentialEndpoint;
79+
PCHAR iotCoreCert;
80+
PCHAR iotCorePrivateKey;
81+
PCHAR iotCoreRoleAlias;
82+
PCHAR iotCoreThingName;
83+
84+
// Direct AWS credentials
85+
PCHAR accessKey;
86+
PCHAR secretKey;
87+
PCHAR sessionToken;
88+
89+
// Common AWS options
90+
PCHAR region;
91+
PCHAR caCertPath;
92+
UINT32 logLevel;
93+
} AwsCredentialOptions, *PAwsCredentialOptions;
94+
7295
#define DATA_CHANNEL_MESSAGE_TEMPLATE \
7396
"{\"content\":\"%s\",\"firstMessageFromViewerTs\":\"%s\",\"firstMessageFromMasterTs\":\"%s\",\"secondMessageFromViewerTs\":\"%s\"," \
7497
"\"secondMessageFromMasterTs\":\"%s\",\"lastMessageFromViewerTs\":\"%s\" }"
@@ -171,6 +194,9 @@ typedef struct {
171194
PCHAR rtspUri;
172195
UINT32 logLevel;
173196
BOOL enableTwcc;
197+
198+
// AWS credential options
199+
PAwsCredentialOptions pAwsCredentialOptions;
174200
} SampleConfiguration, *PSampleConfiguration;
175201

176202
typedef struct {
@@ -242,7 +268,7 @@ PVOID sampleReceiveAudioVideoFrame(PVOID);
242268
PVOID getPeriodicIceCandidatePairStats(PVOID);
243269
STATUS getIceCandidatePairStatsCallback(UINT32, UINT64, UINT64);
244270
STATUS pregenerateCertTimerCallback(UINT32, UINT64, UINT64);
245-
STATUS createSampleConfiguration(PCHAR, SIGNALING_CHANNEL_ROLE_TYPE, BOOL, BOOL, UINT32, PSampleConfiguration*);
271+
STATUS createSampleConfiguration(PCHAR, SIGNALING_CHANNEL_ROLE_TYPE, BOOL, BOOL, UINT32, PAwsCredentialOptions, PSampleConfiguration*);
246272
STATUS freeSampleConfiguration(PSampleConfiguration*);
247273
STATUS signalingClientStateChanged(UINT64, SIGNALING_CLIENT_STATE);
248274
STATUS signalingMessageReceived(UINT64, PReceivedSignalingMessage);

esp_port/examples/app_common/src/sample_config.c

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -910,34 +910,54 @@ STATUS lookForSslCert(PSampleConfiguration* ppSampleConfiguration)
910910
}
911911

912912
STATUS createSampleConfiguration(PCHAR channelName, SIGNALING_CHANNEL_ROLE_TYPE roleType, BOOL trickleIce, BOOL useTurn, UINT32 logLevel,
913-
PSampleConfiguration* ppSampleConfiguration)
913+
PAwsCredentialOptions pAwsCredentialOptions, PSampleConfiguration* ppSampleConfiguration)
914914
{
915915
STATUS retStatus = STATUS_SUCCESS;
916916
PSampleConfiguration pSampleConfiguration = NULL;
917+
PCHAR pAccessKey = NULL, pSecretKey = NULL, pSessionToken = NULL;
918+
PCHAR pIotCoreCredentialEndPoint = NULL, pIotCoreCert = NULL, pIotCorePrivateKey = NULL;
919+
PCHAR pIotCoreRoleAlias = NULL, pIotCoreCertificateId = NULL, pIotCoreThingName = NULL;
917920

918921
CHK(ppSampleConfiguration != NULL, STATUS_NULL_ARG);
919922

920923
CHK(NULL != (pSampleConfiguration = (PSampleConfiguration) MEMCALLOC(1, SIZEOF(SampleConfiguration))), STATUS_NOT_ENOUGH_MEMORY);
921924

922-
#ifdef CONFIG_IOT_CORE_ENABLE_CREDENTIALS
923-
PCHAR pIotCoreCredentialEndPoint, pIotCoreCert, pIotCorePrivateKey, pIotCoreRoleAlias, pIotCoreCertificateId, pIotCoreThingName;
924-
CHK_ERR((pIotCoreCredentialEndPoint = GETENV(IOT_CORE_CREDENTIAL_ENDPOINT)) != NULL, STATUS_INVALID_OPERATION,
925-
"AWS_IOT_CORE_CREDENTIAL_ENDPOINT must be set");
926-
CHK_ERR((pIotCoreCert = GETENV(IOT_CORE_CERT)) != NULL, STATUS_INVALID_OPERATION, "AWS_IOT_CORE_CERT must be set");
927-
CHK_ERR((pIotCorePrivateKey = GETENV(IOT_CORE_PRIVATE_KEY)) != NULL, STATUS_INVALID_OPERATION, "AWS_IOT_CORE_PRIVATE_KEY must be set");
928-
CHK_ERR((pIotCoreRoleAlias = GETENV(IOT_CORE_ROLE_ALIAS)) != NULL, STATUS_INVALID_OPERATION, "AWS_IOT_CORE_ROLE_ALIAS must be set");
929-
CHK_ERR((pIotCoreThingName = GETENV(IOT_CORE_THING_NAME)) != NULL, STATUS_INVALID_OPERATION, "AWS_IOT_CORE_THING_NAME must be set");
930-
#else
931-
PCHAR pAccessKey, pSecretKey, pSessionToken;
932-
CHK_ERR((pAccessKey = GETENV(ACCESS_KEY_ENV_VAR)) != NULL, STATUS_INVALID_OPERATION, "AWS_ACCESS_KEY_ID must be set");
933-
CHK_ERR((pSecretKey = GETENV(SECRET_KEY_ENV_VAR)) != NULL, STATUS_INVALID_OPERATION, "AWS_SECRET_ACCESS_KEY must be set");
934-
935-
pSessionToken = GETENV(SESSION_TOKEN_ENV_VAR);
936-
if (pSessionToken != NULL && IS_EMPTY_STRING(pSessionToken)) {
937-
DLOGW("Session token is set but its value is empty. Ignoring.");
938-
pSessionToken = NULL;
925+
// Store the AWS credential options in the sample configuration
926+
pSampleConfiguration->pAwsCredentialOptions = pAwsCredentialOptions;
927+
928+
if (pAwsCredentialOptions != NULL) {
929+
if (pAwsCredentialOptions->enableIotCredentials) {
930+
// Use IoT Core credentials from the options
931+
pIotCoreCredentialEndPoint = pAwsCredentialOptions->iotCoreCredentialEndpoint;
932+
pIotCoreCert = pAwsCredentialOptions->iotCoreCert;
933+
pIotCorePrivateKey = pAwsCredentialOptions->iotCorePrivateKey;
934+
pIotCoreRoleAlias = pAwsCredentialOptions->iotCoreRoleAlias;
935+
pIotCoreThingName = pAwsCredentialOptions->iotCoreThingName;
936+
// Validate required fields
937+
CHK_ERR(pIotCoreCredentialEndPoint != NULL && pIotCoreCredentialEndPoint[0] != '\0', STATUS_INVALID_OPERATION,
938+
"IoT Core credential endpoint must be set");
939+
CHK_ERR(pIotCoreCert != NULL && pIotCoreCert[0] != '\0', STATUS_INVALID_OPERATION,
940+
"IoT Core certificate must be set");
941+
CHK_ERR(pIotCorePrivateKey != NULL && pIotCorePrivateKey[0] != '\0', STATUS_INVALID_OPERATION,
942+
"IoT Core private key must be set");
943+
CHK_ERR(pIotCoreRoleAlias != NULL && pIotCoreRoleAlias[0] != '\0', STATUS_INVALID_OPERATION,
944+
"IoT Core role alias must be set");
945+
CHK_ERR(pIotCoreThingName != NULL && pIotCoreThingName[0] != '\0', STATUS_INVALID_OPERATION,
946+
"IoT Core thing name must be set");
947+
} else {
948+
// Use direct AWS credentials from the options
949+
pAccessKey = pAwsCredentialOptions->accessKey;
950+
pSecretKey = pAwsCredentialOptions->secretKey;
951+
pSessionToken = pAwsCredentialOptions->sessionToken;
952+
// Validate required fields
953+
CHK_ERR(pAccessKey != NULL && pAccessKey[0] != '\0', STATUS_INVALID_OPERATION,
954+
"AWS access key must be set");
955+
CHK_ERR(pSecretKey != NULL && pSecretKey[0] != '\0', STATUS_INVALID_OPERATION,
956+
"AWS secret key must be set");
957+
}
958+
} else {
959+
DLOGI("Streaming only mode, skipping credentials");
939960
}
940-
#endif
941961

942962

943963
// If the env is set, we generate normal log files apart from filtered profile log files
@@ -969,13 +989,14 @@ STATUS createSampleConfiguration(PCHAR channelName, SIGNALING_CHANNEL_ROLE_TYPE
969989
// CHK_STATUS(lookForSslCert(&pSampleConfiguration));
970990
pSampleConfiguration->pCaCertPath = DEFAULT_KVS_CACERT_PATH;
971991

972-
#ifdef CONFIG_IOT_CORE_ENABLE_CREDENTIALS
973-
CHK_STATUS(createIotCredentialProvider(pIotCoreCredentialEndPoint, pIotCoreCert, pIotCorePrivateKey, pSampleConfiguration->pCaCertPath,
974-
pIotCoreRoleAlias, pIotCoreThingName, &pSampleConfiguration->pCredentialProvider));
975-
#else
976-
CHK_STATUS(
977-
createStaticCredentialProvider(pAccessKey, 0, pSecretKey, 0, pSessionToken, 0, MAX_UINT64, &pSampleConfiguration->pCredentialProvider));
978-
#endif
992+
if (pAwsCredentialOptions != NULL &&
993+
pAwsCredentialOptions->enableIotCredentials) {
994+
CHK_STATUS(createIotCredentialProvider(pIotCoreCredentialEndPoint, pIotCoreCert, pIotCorePrivateKey, pSampleConfiguration->pCaCertPath,
995+
pIotCoreRoleAlias, pIotCoreThingName, &pSampleConfiguration->pCredentialProvider));
996+
} else {
997+
CHK_STATUS(
998+
createStaticCredentialProvider(pAccessKey, 0, pSecretKey, 0, pSessionToken, 0, MAX_UINT64, &pSampleConfiguration->pCredentialProvider));
999+
}
9791000

9801001
pSampleConfiguration->mediaSenderTid = INVALID_TID_VALUE;
9811002
pSampleConfiguration->audioSenderTid = INVALID_TID_VALUE;

esp_port/examples/webrtc_classic/main/webrtc_main.c

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -196,33 +196,56 @@ void app_main(void)
196196
return;
197197
}
198198

199+
// Create AWS credential options structure
200+
AwsCredentialOptions awsCredentialOptions;
201+
memset(&awsCredentialOptions, 0, sizeof(AwsCredentialOptions));
202+
199203
// Initialize KVS WebRTC
200204
STATUS status = STATUS_SUCCESS;
201205
PSampleConfiguration pSampleConfiguration = NULL;
202206
PCHAR pChannelName = CONFIG_AWS_KVS_CHANNEL_NAME;
203207

204208
// Set AWS credentials
205209
#ifdef CONFIG_IOT_CORE_ENABLE_CREDENTIALS
206-
setenv("AWS_IOT_CORE_CREDENTIAL_ENDPOINT", CONFIG_AWS_IOT_CORE_CREDENTIAL_ENDPOINT, 1);
207-
setenv("AWS_IOT_CORE_CERT", CONFIG_AWS_IOT_CORE_CERT, 1);
208-
setenv("AWS_IOT_CORE_PRIVATE_KEY", CONFIG_AWS_IOT_CORE_PRIVATE_KEY, 1);
209-
setenv("AWS_IOT_CORE_ROLE_ALIAS", CONFIG_AWS_IOT_CORE_ROLE_ALIAS, 1);
210-
setenv("AWS_IOT_CORE_THING_NAME", CONFIG_AWS_IOT_CORE_THING_NAME, 1);
210+
// Configure IoT Core credentials
211+
awsCredentialOptions.enableIotCredentials = TRUE;
212+
awsCredentialOptions.iotCoreCredentialEndpoint = CONFIG_AWS_IOT_CORE_CREDENTIAL_ENDPOINT;
213+
awsCredentialOptions.iotCoreCert = CONFIG_AWS_IOT_CORE_CERT;
214+
awsCredentialOptions.iotCorePrivateKey = CONFIG_AWS_IOT_CORE_PRIVATE_KEY;
215+
awsCredentialOptions.iotCoreRoleAlias = CONFIG_AWS_IOT_CORE_ROLE_ALIAS;
216+
awsCredentialOptions.iotCoreThingName = CONFIG_AWS_IOT_CORE_THING_NAME;
217+
218+
setenv("AWS_IOT_CORE_CREDENTIAL_ENDPOINT", "c3mh3f5xs9l81c.credentials.iot.us-east-1.amazonaws.com", 1);
219+
setenv("AWS_IOT_CORE_CERT", "/spiffs/certs/certificate.pem", 1);
220+
setenv("AWS_IOT_CORE_PRIVATE_KEY", "/spiffs/certs/private.key", 1);
221+
setenv("AWS_KVS_CACERT_PATH", "/spiffs/certs/", 1);
222+
setenv("AWS_IOT_CORE_ROLE_ALIAS", "webrtc_iot_role_alias", 1);
223+
setenv("AWS_IOT_CORE_THING_NAME", "webrtc_iot_thing", 1);
211224
#else
212-
setenv("AWS_ACCESS_KEY_ID", CONFIG_AWS_ACCESS_KEY_ID, 1);
213-
setenv("AWS_SECRET_ACCESS_KEY", CONFIG_AWS_SECRET_ACCESS_KEY, 1);
214-
setenv("AWS_DEFAULT_REGION", CONFIG_AWS_DEFAULT_REGION, 1);
225+
// Configure direct AWS credentials
226+
awsCredentialOptions.enableIotCredentials = FALSE;
227+
awsCredentialOptions.accessKey = CONFIG_AWS_ACCESS_KEY_ID;
228+
awsCredentialOptions.secretKey = CONFIG_AWS_SECRET_ACCESS_KEY;
229+
awsCredentialOptions.sessionToken = CONFIG_AWS_SESSION_TOKEN;
215230
#endif
231+
// Set common AWS options
232+
awsCredentialOptions.region = CONFIG_AWS_DEFAULT_REGION;
233+
awsCredentialOptions.caCertPath = "/spiffs/certs/";
234+
awsCredentialOptions.logLevel = 1;
235+
236+
setenv("AWS_KVS_LOG_LEVEL", "1", 1);
237+
setenv("AWS_DEFAULT_REGION", "us-east-1", 1);
216238

217239
// Custom allocator for libwebsockets
218240
lws_set_allocator(realloc_wrapper);
219241

220242
UINT32 logLevel = setLogLevel();
221243

222244
ESP_LOGI(TAG, "creating sample configuration");
223-
// Initialize KVS WebRTC
245+
246+
// Initialize KVS WebRTC with credential options
224247
status = createSampleConfiguration(pChannelName, SIGNALING_CHANNEL_ROLE_TYPE_MASTER,
225-
TRUE, TRUE, logLevel, &pSampleConfiguration);
248+
TRUE, TRUE, logLevel, &awsCredentialOptions, &pSampleConfiguration);
226249
if (status != STATUS_SUCCESS) {
227250
ESP_LOGE(TAG, "createSampleConfiguration failed with 0x%08" PRIx32, status);
228251
goto CleanUp;

0 commit comments

Comments
 (0)