-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add OIDC k8s provider #1576
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add OIDC k8s provider #1576
Changes from 1 commit
7e40dbd
ea8ebcc
2da36e1
d7f9c34
4d4ea3a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,46 @@ elif [ $OIDC_ENV == "azure" ]; then | |
source ./env.sh | ||
elif [ $OIDC_ENV == "gcp" ]; then | ||
source ./secrets-export.sh | ||
elif [ $OIDC_ENV == "k8s" ]; then | ||
# Make sure K8S_VARIANT is set. | ||
if [ -z "$K8S_VARIANT" ]; then | ||
echo "Must specify K8S_VARIANT" | ||
popd | ||
exit 1 | ||
fi | ||
|
||
if [ -z "${VARIANT}" ]; then | ||
echo "VARIANT is not set" | ||
exit 1 | ||
elif [ $VARIANT == "eks" ]; then | ||
path="${AWS_WEB_IDENTITY_TOKEN_FILE}" | ||
elif [ $VARIANT == "aks" ]; then | ||
path="${AZURE_FEDERATED_TOKEN_FILE}" | ||
elif [ $VARIANT == "gke" ]; then | ||
path="/var/run/secrets/kubernetes.io/serviceaccount/token" | ||
else | ||
echo "Unrecognized k8s VARIANT: $VARIANT" | ||
exit 1 | ||
fi | ||
|
||
# Print the file | ||
if [ -f "$path" ]; then | ||
file_size=$(stat -c%s "$path") | ||
echo "VARIANT: $VARIANT" | ||
echo "Token file path: $path" | ||
echo "Token file size: $file_size bytes" | ||
else | ||
echo "Error: Token file not found at $path" >&2 | ||
exit 1 | ||
fi | ||
|
||
if [ $VARIANT == "gke" ]; then | ||
echo "Skipping gke test to avoid error code 137 when running gradle" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Based on our previous investigation, I ran diagnostics on the failing GKE pod and found that the provisioned ephemeral storage was insufficient for Gradle to store dependencies. Diagnostics can be found here: Diagnostic logs. I increased the storage from 2GB to 4GB, and the tests seem to have executed successfully: Successful run. Based on this, I’ve opened a PR with the fix in drivers-evergreen-tools: PR #598. This should resolve the issue with pod execution. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great! Let's push the diagnostic changes up as a PR? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will block this on the other PR |
||
exit 0 | ||
fi | ||
|
||
# fix for git permissions issue: | ||
git config --global --add safe.directory /tmp/test | ||
else | ||
echo "Unrecognized OIDC_ENV $OIDC_ENV" | ||
exit 1 | ||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -38,6 +38,7 @@ | |||
import org.bson.BsonDocument; | ||||
import org.bson.BsonString; | ||||
import org.bson.RawBsonDocument; | ||||
import org.jetbrains.annotations.NotNull; | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
|
||||
import javax.security.sasl.SaslClient; | ||||
import java.io.IOException; | ||||
|
@@ -76,10 +77,11 @@ public final class OidcAuthenticator extends SaslAuthenticator { | |||
private static final String TEST_ENVIRONMENT = "test"; | ||||
private static final String AZURE_ENVIRONMENT = "azure"; | ||||
private static final String GCP_ENVIRONMENT = "gcp"; | ||||
private static final String K8S_ENVIRONMENT = "k8s"; | ||||
private static final List<String> IMPLEMENTED_ENVIRONMENTS = Arrays.asList( | ||||
AZURE_ENVIRONMENT, GCP_ENVIRONMENT, TEST_ENVIRONMENT); | ||||
AZURE_ENVIRONMENT, GCP_ENVIRONMENT, K8S_ENVIRONMENT, TEST_ENVIRONMENT); | ||||
private static final List<String> USER_SUPPORTED_ENVIRONMENTS = Arrays.asList( | ||||
AZURE_ENVIRONMENT, GCP_ENVIRONMENT); | ||||
AZURE_ENVIRONMENT, GCP_ENVIRONMENT, K8S_ENVIRONMENT); | ||||
private static final List<String> REQUIRES_TOKEN_RESOURCE = Arrays.asList( | ||||
AZURE_ENVIRONMENT, GCP_ENVIRONMENT); | ||||
private static final List<String> ALLOWS_USERNAME = Arrays.asList( | ||||
|
@@ -90,6 +92,10 @@ public final class OidcAuthenticator extends SaslAuthenticator { | |||
|
||||
public static final String OIDC_TOKEN_FILE = "OIDC_TOKEN_FILE"; | ||||
|
||||
private static final String K8S_FALLBACK_FILE = "/var/run/secrets/kubernetes.io/serviceaccount/token"; | ||||
private static final String K8S_AZURE_FILE = "AZURE_FEDERATED_TOKEN_FILE"; | ||||
private static final String K8S_AWS_FILE = "AWS_WEB_IDENTITY_TOKEN_FILE"; | ||||
|
||||
private static final int CALLBACK_API_VERSION_NUMBER = 1; | ||||
|
||||
@Nullable | ||||
|
@@ -192,6 +198,8 @@ private OidcCallback getRequestCallback() { | |||
machine = getAzureCallback(getMongoCredential()); | ||||
} else if (GCP_ENVIRONMENT.equals(environment)) { | ||||
machine = getGcpCallback(getMongoCredential()); | ||||
} else if (K8S_ENVIRONMENT.equals(environment)) { | ||||
machine = getK8sCallback(); | ||||
} else { | ||||
machine = getOidcCallbackMechanismProperty(OIDC_CALLBACK_KEY); | ||||
} | ||||
|
@@ -206,6 +214,24 @@ private static OidcCallback getTestCallback() { | |||
}; | ||||
} | ||||
|
||||
@VisibleForTesting(otherwise = VisibleForTesting.AccessModifier.PRIVATE) | ||||
static OidcCallback getK8sCallback() { | ||||
return (context) -> { | ||||
String azure = System.getenv(K8S_AZURE_FILE); | ||||
String aws = System.getenv(K8S_AWS_FILE); | ||||
String path; | ||||
if (azure != null) { | ||||
path = azure; | ||||
} else if (aws != null) { | ||||
path = aws; | ||||
} else { | ||||
path = K8S_FALLBACK_FILE; | ||||
} | ||||
String accessToken = readTokenFromFile(path); | ||||
return new OidcCallbackResult(accessToken); | ||||
}; | ||||
} | ||||
|
||||
@VisibleForTesting(otherwise = VisibleForTesting.AccessModifier.PRIVATE) | ||||
static OidcCallback getAzureCallback(final MongoCredential credential) { | ||||
return (context) -> { | ||||
|
@@ -499,6 +525,11 @@ private static String readTokenFromFile() { | |||
throw new MongoClientException( | ||||
format("Environment variable must be specified: %s", OIDC_TOKEN_FILE)); | ||||
} | ||||
return readTokenFromFile(path); | ||||
} | ||||
|
||||
@NotNull | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
private static String readTokenFromFile(final String path) { | ||||
try { | ||||
return new String(Files.readAllBytes(Paths.get(path)), StandardCharsets.UTF_8); | ||||
} catch (IOException e) { | ||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -261,7 +261,14 @@ static class ShutdownHook extends Thread { | |
@Override | ||
public void run() { | ||
if (cluster != null) { | ||
new DropDatabaseOperation(getDefaultDatabaseName(), WriteConcern.ACKNOWLEDGED).execute(getBinding()); | ||
try { | ||
new DropDatabaseOperation(getDefaultDatabaseName(), WriteConcern.ACKNOWLEDGED).execute(getBinding()); | ||
} catch (MongoCommandException e) { | ||
// if we do not have permission to drop the database, assume it is cleaned up in some other way | ||
if (!e.getMessage().contains("Command dropDatabase requires authentication")) { | ||
throw e; | ||
} | ||
} | ||
cluster.close(); | ||
Comment on lines
-264
to
272
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As of this PR, we will sometimes lack permission to drop the database. |
||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest we break this into separate tasks and list them in the
test-oidc-k8s-task-group
task group. This would allow us to: