File: shared_credentials_test_definitions.h

package info (click to toggle)
aws-crt-python 0.20.4%2Bdfsg-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 72,656 kB
  • sloc: ansic: 381,805; python: 23,008; makefile: 6,251; sh: 4,536; cpp: 699; ruby: 208; java: 77; perl: 73; javascript: 46; xml: 11
file content (73 lines) | stat: -rw-r--r-- 2,666 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#ifndef SHARED_CREDENTIALS_TEST_DEFINITIONS_H
#define SHARED_CREDENTIALS_TEST_DEFINITIONS_H
/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

#include <aws/common/string.h>
#include <aws/common/uuid.h>
#include <aws/io/file_utils.h>

#include <errno.h>

#ifdef _MSC_VER
/* fopen, fprintf etc... */
#    pragma warning(push)
#    pragma warning(disable : 4996)
#endif

AWS_STATIC_STRING_FROM_LITERAL(s_default_profile_env_variable_name, "AWS_PROFILE");
AWS_STATIC_STRING_FROM_LITERAL(s_default_config_path_env_variable_name, "AWS_CONFIG_FILE");
AWS_STATIC_STRING_FROM_LITERAL(s_default_credentials_path_env_variable_name, "AWS_SHARED_CREDENTIALS_FILE");
AWS_STATIC_STRING_FROM_LITERAL(s_access_key_id_env_var, "AWS_ACCESS_KEY_ID");
AWS_STATIC_STRING_FROM_LITERAL(s_secret_access_key_env_var, "AWS_SECRET_ACCESS_KEY");
AWS_STATIC_STRING_FROM_LITERAL(s_session_token_env_var, "AWS_SESSION_TOKEN");

static struct aws_string *aws_create_process_unique_file_name(struct aws_allocator *allocator) {
    char file_name_storage[64] = {0};
    struct aws_byte_buf filename_buf = aws_byte_buf_from_empty_array(file_name_storage, sizeof(file_name_storage));

#ifndef WIN32
    AWS_FATAL_ASSERT(aws_byte_buf_write_from_whole_cursor(&filename_buf, aws_byte_cursor_from_c_str("./")));
#endif

    AWS_FATAL_ASSERT(
        aws_byte_buf_write_from_whole_cursor(&filename_buf, aws_byte_cursor_from_c_str("config_creds_test")));

    struct aws_uuid uuid;
    AWS_FATAL_ASSERT(aws_uuid_init(&uuid) == AWS_OP_SUCCESS);
    AWS_FATAL_ASSERT(aws_uuid_to_str(&uuid, &filename_buf) == AWS_OP_SUCCESS);

    return aws_string_new_from_array(allocator, filename_buf.buffer, filename_buf.len);
}

static int aws_create_profile_file(const struct aws_string *file_name, const struct aws_string *file_contents) {
    /* avoid compiler warning if some files include this header but don't actually use those variables */
    (void)s_default_profile_env_variable_name;
    (void)s_default_config_path_env_variable_name;
    (void)s_default_credentials_path_env_variable_name;
    (void)s_access_key_id_env_var;
    (void)s_secret_access_key_env_var;
    (void)s_session_token_env_var;

    FILE *fp = fopen(aws_string_c_str(file_name), "w");
    if (fp == NULL) {
        return aws_translate_and_raise_io_error(errno);
    }

    int result = fprintf(fp, "%s", aws_string_c_str(file_contents));
    fclose(fp);

    if (result < 0) {
        return aws_translate_and_raise_io_error(errno);
    }

    return AWS_OP_SUCCESS;
}

#ifdef _MSC_VER
#    pragma warning(pop)
#endif

#endif /* SHARED_CREDENTIALS_TEST_DEFINITIONS_H */