Skip to content

Commit e22ab0c

Browse files
authored
CDRIVER-4489 OIDC environment validation and callback stubs (#2002)
* This is a small step toward CDRIVER-4489, it does not fully resolve any issues. * Adds an internal mongoc_oidc_env interface * Refactors common $external defaulting code * Updates URI validation (mongoc_uri_finalize_auth) for OIDC support * Sync new URI tests from specifications commit bc988bb1e234bf22407e166c3b69c8a98bfac48d * Updates test skips
1 parent fa70282 commit e22ab0c

File tree

6 files changed

+346
-39
lines changed

6 files changed

+346
-39
lines changed

src/libmongoc/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,7 @@ set (MONGOC_SOURCES
598598
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-memcmp.c
599599
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-cmd.c
600600
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-oidc-callback.c
601+
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-oidc-env.c
601602
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-opcode.c
602603
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-optional.c
603604
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-opts-helpers.c
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2009-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <mongoc/mongoc-prelude.h>
18+
19+
#ifndef MONGOC_OIDC_ENV_PRIVATE_H
20+
#define MONGOC_OIDC_ENV_PRIVATE_H
21+
22+
#include <mongoc/mongoc-macros.h>
23+
#include <mongoc/mongoc-oidc-callback.h>
24+
25+
BSON_BEGIN_DECLS
26+
27+
typedef struct _mongoc_oidc_env_t mongoc_oidc_env_t;
28+
typedef struct _mongoc_oidc_env_callback_t mongoc_oidc_env_callback_t;
29+
30+
const mongoc_oidc_env_t *
31+
mongoc_oidc_env_find (const char *name);
32+
33+
const char *
34+
mongoc_oidc_env_name (const mongoc_oidc_env_t *env);
35+
36+
bool
37+
mongoc_oidc_env_supports_username (const mongoc_oidc_env_t *env);
38+
39+
bool
40+
mongoc_oidc_env_requires_token_resource (const mongoc_oidc_env_t *env);
41+
42+
mongoc_oidc_env_callback_t *
43+
mongoc_oidc_env_callback_new (const mongoc_oidc_env_t *env, const char *token_resource);
44+
45+
void
46+
mongoc_oidc_env_callback_destroy (mongoc_oidc_env_callback_t *env_callback);
47+
48+
const mongoc_oidc_callback_t *
49+
mongoc_oidc_env_callback_inner (const mongoc_oidc_env_callback_t *env_callback);
50+
51+
BSON_END_DECLS
52+
53+
#endif // MONGOC_OIDC_ENV_PRIVATE_H
+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*
2+
* Copyright 2009-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <mongoc/mongoc-oidc-env-private.h>
18+
#include <mongoc/mongoc-oidc-callback.h>
19+
20+
struct _mongoc_oidc_env_t {
21+
const char *name;
22+
mongoc_oidc_callback_fn_t callback_fn;
23+
bool supports_username;
24+
bool requires_token_resource;
25+
};
26+
27+
struct _mongoc_oidc_env_callback_t {
28+
mongoc_oidc_callback_t *inner; // Contains non-owning user_data pointer back to this mongoc_oidc_env_callback_t
29+
char *token_resource;
30+
};
31+
32+
static mongoc_oidc_credential_t *
33+
mongoc_oidc_env_fn_test (mongoc_oidc_callback_params_t *params)
34+
{
35+
BSON_UNUSED (params);
36+
// TODO (CDRIVER-4489)
37+
return NULL;
38+
}
39+
40+
static mongoc_oidc_credential_t *
41+
mongoc_oidc_env_fn_azure (mongoc_oidc_callback_params_t *params)
42+
{
43+
BSON_UNUSED (params);
44+
// TODO (CDRIVER-4489)
45+
return NULL;
46+
}
47+
48+
static mongoc_oidc_credential_t *
49+
mongoc_oidc_env_fn_gcp (mongoc_oidc_callback_params_t *params)
50+
{
51+
BSON_UNUSED (params);
52+
// TODO (CDRIVER-4489)
53+
return NULL;
54+
}
55+
56+
static mongoc_oidc_credential_t *
57+
mongoc_oidc_env_fn_k8s (mongoc_oidc_callback_params_t *params)
58+
{
59+
BSON_UNUSED (params);
60+
// TODO (CDRIVER-4489)
61+
return NULL;
62+
}
63+
64+
const mongoc_oidc_env_t *
65+
mongoc_oidc_env_find (const char *name)
66+
{
67+
static const mongoc_oidc_env_t oidc_env_table[] = {
68+
{.name = "test", .callback_fn = mongoc_oidc_env_fn_test},
69+
{.name = "azure",
70+
.supports_username = true,
71+
.requires_token_resource = true,
72+
.callback_fn = mongoc_oidc_env_fn_azure},
73+
{.name = "gcp", .requires_token_resource = true, .callback_fn = mongoc_oidc_env_fn_gcp},
74+
{.name = "k8s", .callback_fn = mongoc_oidc_env_fn_k8s},
75+
{0}};
76+
77+
if (name) {
78+
for (const mongoc_oidc_env_t *row = oidc_env_table; row->name; ++row) {
79+
if (!strcmp (name, row->name)) {
80+
return row;
81+
}
82+
}
83+
}
84+
return NULL;
85+
}
86+
87+
const char *
88+
mongoc_oidc_env_name (const mongoc_oidc_env_t *env)
89+
{
90+
BSON_ASSERT_PARAM (env);
91+
return env->name;
92+
}
93+
94+
bool
95+
mongoc_oidc_env_supports_username (const mongoc_oidc_env_t *env)
96+
{
97+
BSON_ASSERT_PARAM (env);
98+
return env->supports_username;
99+
}
100+
101+
bool
102+
mongoc_oidc_env_requires_token_resource (const mongoc_oidc_env_t *env)
103+
{
104+
BSON_ASSERT_PARAM (env);
105+
return env->requires_token_resource;
106+
}
107+
108+
mongoc_oidc_env_callback_t *
109+
mongoc_oidc_env_callback_new (const mongoc_oidc_env_t *env, const char *token_resource)
110+
{
111+
BSON_ASSERT_PARAM (env);
112+
BSON_OPTIONAL_PARAM (token_resource);
113+
mongoc_oidc_env_callback_t *env_callback = bson_malloc (sizeof *env_callback);
114+
// Note that the callback's user_data points back to this containing mongoc_oidc_env_callback_t.
115+
// We expect that the inner callback can only be destroyed via mongoc_oidc_env_callback_destroy.
116+
*env_callback =
117+
(mongoc_oidc_env_callback_t){.inner = mongoc_oidc_callback_new_with_user_data (env->callback_fn, env_callback),
118+
.token_resource = bson_strdup (token_resource)};
119+
return env_callback;
120+
}
121+
122+
void
123+
mongoc_oidc_env_callback_destroy (mongoc_oidc_env_callback_t *env_callback)
124+
{
125+
if (env_callback) {
126+
BSON_ASSERT (mongoc_oidc_callback_get_user_data (env_callback->inner) == (void *) env_callback);
127+
mongoc_oidc_callback_destroy (env_callback->inner);
128+
bson_free (env_callback->token_resource);
129+
bson_free (env_callback);
130+
}
131+
}
132+
133+
const mongoc_oidc_callback_t *
134+
mongoc_oidc_env_callback_inner (const mongoc_oidc_env_callback_t *env_callback)
135+
{
136+
BSON_ASSERT_PARAM (env_callback);
137+
return env_callback->inner;
138+
}

0 commit comments

Comments
 (0)