-
Notifications
You must be signed in to change notification settings - Fork 533
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RUBY-3303 Add OIDC machine workflow auth
- Loading branch information
Showing
34 changed files
with
1,714 additions
and
378 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#!/bin/bash | ||
|
||
set -ex | ||
|
||
ENVIRONMENT=${ENVIRONMENT:-"test"} | ||
|
||
. `dirname "$0"`/../spec/shared/shlib/distro.sh | ||
. `dirname "$0"`/../spec/shared/shlib/set_env.sh | ||
. `dirname "$0"`/functions.sh | ||
|
||
set_env_vars | ||
set_env_python | ||
set_env_ruby | ||
|
||
bundle_install | ||
bundle exec rspec -fd spec/integration/oidc/${ENVIRONMENT}_machine_auth_flow_prose_spec.rb | ||
|
||
test_status=$? | ||
|
||
kill_jruby | ||
|
||
exit ${test_status} |
Empty file.
Submodule drivers-evergreen-tools
updated
56 files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# frozen_string_literal: true | ||
# rubocop:todo all | ||
|
||
# Copyright (C) 2014-2024 MongoDB, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
module Mongo | ||
module Auth | ||
|
||
# Defines behavior for OIDC authentication. | ||
# | ||
# @api private | ||
class Oidc < Base | ||
attr_reader :speculative_auth_result | ||
|
||
# The authentication mechanism string. | ||
# | ||
# @since 2.20.0 | ||
MECHANISM = 'MONGODB-OIDC'.freeze | ||
|
||
# Initializes the OIDC authenticator. | ||
# | ||
# @param [ Auth::User ] user The user to authenticate. | ||
# @param [ Mongo::Connection ] connection The connection to authenticate over. | ||
# | ||
# @option opts [ BSON::Document | nil ] speculative_auth_result The | ||
# value of speculativeAuthenticate field of hello response of | ||
# the handshake on the specified connection. | ||
def initialize(user, connection, **opts) | ||
super | ||
@speculative_auth_result = opts[:speculative_auth_result] | ||
@machine_workflow = MachineWorkflow::new(auth_mech_properties: user.auth_mech_properties) | ||
end | ||
|
||
# Log the user in on the current connection. | ||
# | ||
# @return [ BSON::Document ] The document of the authentication response. | ||
def login | ||
execute_workflow(connection: connection, conversation: conversation) | ||
end | ||
|
||
private | ||
|
||
def execute_workflow(connection:, conversation:) | ||
# If there is a cached access token, try to authenticate with it. If | ||
# authentication fails with an Authentication error (18), | ||
# invalidate the access token, fetch a new access token, and try | ||
# to authenticate again. | ||
# If the server fails for any other reason, do not clear the cache. | ||
if cache.access_token? | ||
token = cache.access_token | ||
msg = conversation.start(connection: connection, token: token) | ||
begin | ||
dispatch_msg(connection, conversation, msg) | ||
rescue AuthError => error | ||
cache.invalidate(token: token) | ||
execute_workflow(connection: connection, conversation: conversation) | ||
end | ||
end | ||
# This is the normal flow when no token is in the cache. Execute the | ||
# machine callback to get the token, put it in the caches, and then | ||
# send the saslStart to the server. | ||
token = machine_workflow.execute | ||
cache.access_token = token | ||
connection.access_token = token | ||
msg = conversation.start(connection: connection, token: token) | ||
dispatch_msg(connection, conversation, msg) | ||
end | ||
end | ||
end | ||
end | ||
|
||
require 'mongo/auth/oidc/conversation' | ||
require 'mongo/auth/oidc/machine_workflow' | ||
require 'mongo/auth/oidc/token_cache' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# frozen_string_literal: true | ||
# rubocop:todo all | ||
|
||
# Copyright (C) 2024 MongoDB Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
module Mongo | ||
module Auth | ||
class Oidc | ||
# Defines behaviour around a single OIDC conversation between the | ||
# client and the server. | ||
# | ||
# @api private | ||
class Conversation < ConversationBase | ||
# The base client message. | ||
START_MESSAGE = { saslStart: 1, mechanism: Oidc::MECHANISM }.freeze | ||
|
||
# Create the new conversation. | ||
# | ||
# @example Create the new conversation. | ||
# Conversation.new(user, 'test.example.com') | ||
# | ||
# @param [ Auth::User ] user The user to converse about. | ||
# @param [ Mongo::Connection ] connection The connection to | ||
# authenticate over. | ||
# | ||
# @since 2.20.0 | ||
def initialize(user, connection, **opts) | ||
super | ||
end | ||
|
||
# OIDC machine workflow is always a saslStart with the payload being | ||
# the serialized jwt token. | ||
# | ||
# @param [ String ] token The access token. | ||
# | ||
# @return [ Hash ] The start document. | ||
def client_start_document(token:) | ||
START_MESSAGE.merge(payload: finish_payload(token: token)) | ||
end | ||
|
||
# Gets the serialized jwt payload for the token. | ||
# | ||
# @param [ String ] token The access token. | ||
# | ||
# @return [ BSON::Binary ] The serialized payload. | ||
def finish_payload(token:) | ||
payload = { jwt: token }.to_bson.to_s | ||
BSON::Binary.new(payload) | ||
end | ||
|
||
# Start the OIDC conversation. This returns the first message that | ||
# needs to be sent to the server. | ||
# | ||
# @param [ Server::Connection ] connection The connection being authenticated. | ||
# | ||
# @return [ Protocol::Message ] The first OIDC conversation message. | ||
def start(connection:, token:) | ||
selector = client_start_document(token: token) | ||
build_message(connection, '$external', selector) | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.