mirror of
https://github.com/openai/codex.git
synced 2026-11-18 22:20:48 +00:00
## Why The app server's Bedrock setup and login flows always selected Mantle. Clients using Bedrock Runtime had to configure that provider separately. ## What changed Accept an optional `providerId` in `account/bedrock/setup` for AWS profiles and environment credentials, and in `account/login/start` for Bedrock API keys and AWS access keys. Pass `"amazon-bedrock-runtime"` to select Runtime. Omit the field or pass `null` to keep the Mantle default. Save configuration for the selected provider and check that provider for an existing `aws.credential_export` before changing credential sources. ## Testing Extend app-server tests to cover Runtime setup with profiles and environment credentials, Runtime login with both key types, and Mantle fallback. Verify that environment setup selects the Runtime default model for a new thread and that a configured Runtime credential exporter blocks profile setup. GitOrigin-RevId: 6396b638208ab9b12f50f13f13bd517a919d0848
832 lines
32 KiB
Rust
832 lines
32 KiB
Rust
//! Amazon Bedrock credential setup and the startup GovCloud guidance screen.
|
|
|
|
use super::auth::AuthModeWidget;
|
|
use super::auth::SignInState;
|
|
use super::auth::onboarding_request_id;
|
|
use super::keys;
|
|
use super::provider_setup::FieldDisplay;
|
|
use super::provider_setup::ProviderInput;
|
|
use super::provider_setup::move_provider_field;
|
|
use crate::key_hint::KeyBindingListExt;
|
|
use crate::wrapping::word_wrap_lines;
|
|
use codex_app_server_client::AppServerRequestHandle;
|
|
use codex_app_server_protocol::AwsCredentialType;
|
|
use codex_app_server_protocol::BedrockAwsProfile;
|
|
use codex_app_server_protocol::BedrockCheckGovCloudRequirementsParams;
|
|
use codex_app_server_protocol::BedrockCheckGovCloudRequirementsResponse;
|
|
use codex_app_server_protocol::BedrockDiscoverParams;
|
|
use codex_app_server_protocol::BedrockDiscoverResponse;
|
|
use codex_app_server_protocol::BedrockEnvironmentCredential;
|
|
use codex_app_server_protocol::BedrockSetupParams;
|
|
use codex_app_server_protocol::BedrockSetupResponse;
|
|
use codex_app_server_protocol::ClientRequest;
|
|
use codex_app_server_protocol::LoginAccountParams;
|
|
use codex_app_server_protocol::LoginAccountResponse;
|
|
use codex_app_server_protocol::RequestId;
|
|
use crossterm::event::KeyCode;
|
|
use crossterm::event::KeyEvent;
|
|
use crossterm::event::KeyEventKind;
|
|
use ratatui::buffer::Buffer;
|
|
use ratatui::layout::Rect;
|
|
use ratatui::prelude::Widget;
|
|
use ratatui::style::Stylize;
|
|
use ratatui::text::Line;
|
|
use ratatui::widgets::Paragraph;
|
|
use std::sync::Arc;
|
|
use std::sync::PoisonError;
|
|
use std::sync::atomic::AtomicUsize;
|
|
use std::sync::atomic::Ordering;
|
|
|
|
const GOV_CLOUD_GUIDANCE_URL: &str =
|
|
"https://learn.chatgpt.com/docs/enterprise/govcloud-configuration";
|
|
|
|
#[derive(Clone)]
|
|
pub(super) struct BedrockState {
|
|
view: BedrockView,
|
|
highlighted: usize,
|
|
profiles: Vec<BedrockAwsProfile>,
|
|
environment_credentials: Vec<BedrockEnvironmentCredential>,
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
enum BedrockView {
|
|
Discovering(RequestId),
|
|
Methods(BedrockMethodList),
|
|
ProfileEntry(ProviderInput),
|
|
AccessKeyEntry {
|
|
values: [ProviderInput; 3],
|
|
selected_field: usize,
|
|
},
|
|
ApiKeyEntry(ProviderInput),
|
|
RegionEntry {
|
|
credential: BedrockCredential,
|
|
value: ProviderInput,
|
|
},
|
|
EnvironmentInstructions,
|
|
Configuring(RequestId),
|
|
// Render-clamped scroll offset shared with the auth widget's cloned state.
|
|
GovCloudWarning(Arc<AtomicUsize>),
|
|
}
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq)]
|
|
enum BedrockMethodList {
|
|
Detected,
|
|
All,
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
enum BedrockCredential {
|
|
Profile(String),
|
|
Environment,
|
|
AccessKeys {
|
|
access_key_id: String,
|
|
secret_access_key: String,
|
|
session_token: Option<String>,
|
|
},
|
|
ApiKey(String),
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
enum BedrockMethod {
|
|
Profile(usize),
|
|
Environment,
|
|
OtherMethods,
|
|
ManualProfile,
|
|
AccessKeys,
|
|
EnvironmentInstructions,
|
|
ApiKey,
|
|
}
|
|
|
|
enum BedrockAction {
|
|
BackToAuth,
|
|
Configure(BedrockCredential, String),
|
|
}
|
|
|
|
impl BedrockState {
|
|
pub(super) fn guidance() -> Self {
|
|
Self {
|
|
view: BedrockView::GovCloudWarning(Arc::default()),
|
|
highlighted: 0,
|
|
profiles: Vec::new(),
|
|
environment_credentials: Vec::new(),
|
|
}
|
|
}
|
|
|
|
pub(super) fn handle_guidance_key_event(&mut self, key_event: &KeyEvent) -> bool {
|
|
let BedrockView::GovCloudWarning(scroll) = &self.view else {
|
|
return false;
|
|
};
|
|
let offset = scroll.load(Ordering::Relaxed);
|
|
if keys::MOVE_UP.is_pressed(*key_event) {
|
|
scroll.store(offset.saturating_sub(1), Ordering::Relaxed);
|
|
} else if keys::MOVE_DOWN.is_pressed(*key_event) {
|
|
scroll.store(offset.saturating_add(1), Ordering::Relaxed);
|
|
}
|
|
key_event.kind == KeyEventKind::Press && keys::CONFIRM.is_pressed(*key_event)
|
|
}
|
|
|
|
fn discovering(request_id: RequestId) -> Self {
|
|
Self {
|
|
view: BedrockView::Discovering(request_id),
|
|
highlighted: 0,
|
|
profiles: Vec::new(),
|
|
environment_credentials: Vec::new(),
|
|
}
|
|
}
|
|
|
|
fn discovered(response: BedrockDiscoverResponse) -> Self {
|
|
Self {
|
|
view: BedrockView::Methods(BedrockMethodList::Detected),
|
|
highlighted: 0,
|
|
profiles: response.profiles,
|
|
environment_credentials: response.environment_credentials,
|
|
}
|
|
}
|
|
|
|
fn methods(&self) -> Vec<BedrockMethod> {
|
|
if matches!(self.view, BedrockView::EnvironmentInstructions) {
|
|
return vec![BedrockMethod::OtherMethods];
|
|
}
|
|
let detected = matches!(self.view, BedrockView::Methods(BedrockMethodList::Detected));
|
|
if detected && !self.profiles.is_empty() {
|
|
let mut methods = (0..self.profiles.len())
|
|
.map(BedrockMethod::Profile)
|
|
.collect::<Vec<_>>();
|
|
if !self.environment_credentials.is_empty() {
|
|
methods.push(BedrockMethod::Environment);
|
|
}
|
|
methods.push(BedrockMethod::OtherMethods);
|
|
return methods;
|
|
}
|
|
if detected && !self.environment_credentials.is_empty() {
|
|
return vec![BedrockMethod::Environment, BedrockMethod::OtherMethods];
|
|
}
|
|
vec![
|
|
BedrockMethod::ManualProfile,
|
|
BedrockMethod::AccessKeys,
|
|
BedrockMethod::EnvironmentInstructions,
|
|
BedrockMethod::ApiKey,
|
|
]
|
|
}
|
|
|
|
pub(super) fn is_text_entry_active(&self) -> bool {
|
|
matches!(
|
|
self.view,
|
|
BedrockView::ProfileEntry(_)
|
|
| BedrockView::AccessKeyEntry { .. }
|
|
| BedrockView::ApiKeyEntry(_)
|
|
| BedrockView::RegionEntry { .. }
|
|
)
|
|
}
|
|
|
|
pub(super) fn is_configuring(&self, id: &RequestId) -> bool {
|
|
matches!(&self.view, BedrockView::Configuring(active) if active == id)
|
|
}
|
|
|
|
pub(super) fn active_input_mut(&mut self) -> Option<&mut ProviderInput> {
|
|
match &mut self.view {
|
|
BedrockView::ProfileEntry(value)
|
|
| BedrockView::ApiKeyEntry(value)
|
|
| BedrockView::RegionEntry { value, .. } => Some(value),
|
|
BedrockView::AccessKeyEntry {
|
|
values,
|
|
selected_field,
|
|
} => Some(&mut values[*selected_field]),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
fn enter_region(&mut self, credential: BedrockCredential, value: &str) {
|
|
self.view = BedrockView::RegionEntry {
|
|
credential,
|
|
value: value.into(),
|
|
};
|
|
}
|
|
|
|
fn select_method(&mut self, method: BedrockMethod) -> Option<BedrockAction> {
|
|
self.highlighted = 0;
|
|
match method {
|
|
BedrockMethod::Profile(index) => {
|
|
let profile = self.profiles[index].clone();
|
|
let credential = BedrockCredential::Profile(profile.name);
|
|
if let Some(region) = profile.region {
|
|
return Some(BedrockAction::Configure(credential, region));
|
|
}
|
|
self.enter_region(credential, "");
|
|
}
|
|
BedrockMethod::Environment => {
|
|
let credential = BedrockCredential::Environment;
|
|
if let Some(region) = self
|
|
.environment_credentials
|
|
.iter()
|
|
.find_map(|credential| credential.region.clone())
|
|
{
|
|
return Some(BedrockAction::Configure(credential, region));
|
|
}
|
|
self.enter_region(credential, "");
|
|
}
|
|
BedrockMethod::OtherMethods => {
|
|
self.view = BedrockView::Methods(BedrockMethodList::All);
|
|
}
|
|
BedrockMethod::ManualProfile => {
|
|
self.view = BedrockView::ProfileEntry(ProviderInput::default());
|
|
}
|
|
BedrockMethod::AccessKeys => {
|
|
self.view = BedrockView::AccessKeyEntry {
|
|
values: Default::default(),
|
|
selected_field: 0,
|
|
};
|
|
}
|
|
BedrockMethod::EnvironmentInstructions => {
|
|
self.view = BedrockView::EnvironmentInstructions;
|
|
}
|
|
BedrockMethod::ApiKey => {
|
|
self.view = BedrockView::ApiKeyEntry(ProviderInput::default());
|
|
}
|
|
}
|
|
None
|
|
}
|
|
|
|
fn handle_key_event(&mut self, key_event: &KeyEvent) -> Option<BedrockAction> {
|
|
if keys::CANCEL.is_pressed(*key_event) {
|
|
let leave_wizard = matches!(
|
|
self.view,
|
|
BedrockView::Discovering(_) | BedrockView::Methods(BedrockMethodList::Detected)
|
|
);
|
|
if leave_wizard {
|
|
return Some(BedrockAction::BackToAuth);
|
|
}
|
|
self.view = BedrockView::Methods(BedrockMethodList::Detected);
|
|
self.highlighted = 0;
|
|
return None;
|
|
}
|
|
if matches!(self.view, BedrockView::Configuring(_)) {
|
|
return None;
|
|
}
|
|
|
|
if matches!(
|
|
self.view,
|
|
BedrockView::Methods(_) | BedrockView::EnvironmentInstructions
|
|
) {
|
|
let methods = self.methods();
|
|
if keys::MOVE_UP.is_pressed(*key_event) {
|
|
self.highlighted = (self.highlighted + methods.len() - 1) % methods.len();
|
|
} else if keys::MOVE_DOWN.is_pressed(*key_event) {
|
|
self.highlighted = (self.highlighted + 1) % methods.len();
|
|
} else if keys::CONFIRM.is_pressed(*key_event) {
|
|
return self.select_method(methods[self.highlighted]);
|
|
} else if let KeyCode::Char(digit @ '1'..='9') = key_event.code
|
|
&& let Some(method) = methods.get(digit as usize - '1' as usize).copied()
|
|
{
|
|
return self.select_method(method);
|
|
}
|
|
return None;
|
|
}
|
|
|
|
if let BedrockView::AccessKeyEntry {
|
|
values,
|
|
selected_field,
|
|
} = &mut self.view
|
|
&& move_provider_field(*key_event, selected_field, values.len())
|
|
{
|
|
return None;
|
|
}
|
|
|
|
if keys::CONFIRM.is_pressed(*key_event) {
|
|
return self.confirm_input();
|
|
}
|
|
if let Some(input) = self.active_input_mut() {
|
|
input.input(*key_event);
|
|
}
|
|
None
|
|
}
|
|
|
|
fn confirm_input(&mut self) -> Option<BedrockAction> {
|
|
let credential = match &mut self.view {
|
|
BedrockView::ProfileEntry(value) => BedrockCredential::Profile(value.value()?),
|
|
BedrockView::ApiKeyEntry(value) => BedrockCredential::ApiKey(value.value()?),
|
|
BedrockView::RegionEntry { credential, value } => {
|
|
return Some(BedrockAction::Configure(credential.clone(), value.value()?));
|
|
}
|
|
BedrockView::AccessKeyEntry {
|
|
values,
|
|
selected_field,
|
|
} => {
|
|
if *selected_field < 2 {
|
|
if values[*selected_field].value().is_some() {
|
|
*selected_field += 1;
|
|
}
|
|
return None;
|
|
}
|
|
let Some(access_key_id) = values[0].value() else {
|
|
*selected_field = 0;
|
|
return None;
|
|
};
|
|
let Some(secret_access_key) = values[1].value() else {
|
|
*selected_field = 1;
|
|
return None;
|
|
};
|
|
BedrockCredential::AccessKeys {
|
|
access_key_id,
|
|
secret_access_key,
|
|
session_token: values[2].value(),
|
|
}
|
|
}
|
|
_ => return None,
|
|
};
|
|
if let BedrockCredential::Profile(profile) = &credential
|
|
&& let Some(region) = self.profiles.iter().find_map(|discovered| {
|
|
(discovered.name == *profile)
|
|
.then(|| discovered.region.clone())
|
|
.flatten()
|
|
})
|
|
{
|
|
return Some(BedrockAction::Configure(credential, region));
|
|
}
|
|
self.enter_region(credential, "");
|
|
None
|
|
}
|
|
|
|
pub(super) fn render(&self, area: Rect, buf: &mut Buffer, error: Option<String>) {
|
|
if area.width == 0 || area.height == 0 {
|
|
return;
|
|
}
|
|
let mut lines: Vec<Line> = vec![
|
|
Line::from(vec!["> ".into(), "Set up Amazon Bedrock".bold()]),
|
|
"".into(),
|
|
];
|
|
let mut footer = Vec::new();
|
|
match &self.view {
|
|
BedrockView::GovCloudWarning(_) => {
|
|
lines = vec!["Using Codex with AWS GovCloud".bold().into(), "".into()];
|
|
lines.push(Line::from(vec![
|
|
"You are using Codex with AWS GovCloud. Please ensure you or your administrator have read the ".into(),
|
|
"application configuration and security guidance"
|
|
.cyan()
|
|
.underlined(),
|
|
" before proceeding.".into(),
|
|
]));
|
|
footer.push("".into());
|
|
footer.push("> Acknowledge".cyan().into());
|
|
}
|
|
BedrockView::Discovering(_) => {
|
|
lines.push(" Checking for existing AWS credentials...".dim().into());
|
|
}
|
|
BedrockView::Configuring(_) => {
|
|
lines.push(" Setting up Amazon Bedrock...".dim().into());
|
|
}
|
|
BedrockView::Methods(list) => {
|
|
if *list == BedrockMethodList::Detected && self.profiles.len() == 1 {
|
|
let profile = &self.profiles[0];
|
|
lines.push(format!(" AWS profile detected: {}", profile.name).into());
|
|
if let Some(region) = &profile.region {
|
|
lines.push(format!(" Region: {region}").dim().into());
|
|
}
|
|
} else if *list == BedrockMethodList::Detected && self.profiles.len() > 1 {
|
|
lines.push(" Choose an AWS profile.".into());
|
|
} else if *list == BedrockMethodList::Detected
|
|
&& !self.environment_credentials.is_empty()
|
|
{
|
|
lines.push(" AWS credentials detected in the server environment.".into());
|
|
} else {
|
|
if *list == BedrockMethodList::Detected {
|
|
lines.push(" No AWS credentials found.".into());
|
|
}
|
|
lines.push(" Choose how you authenticate with AWS.".into());
|
|
}
|
|
lines.push("".into());
|
|
self.render_methods(&mut lines);
|
|
}
|
|
BedrockView::ProfileEntry(value) => {
|
|
lines.push(" Enter the name of your AWS profile.".into());
|
|
lines.push("".into());
|
|
lines.push(value.line(" AWS profile: ", "", FieldDisplay::Focused, area.width));
|
|
}
|
|
BedrockView::ApiKeyEntry(value) => {
|
|
lines.push(" Enter your Amazon Bedrock API key.".into());
|
|
lines.push("".into());
|
|
lines.push(value.line(
|
|
" Bedrock API key: ",
|
|
"",
|
|
FieldDisplay::FocusedSecret,
|
|
area.width,
|
|
));
|
|
}
|
|
BedrockView::RegionEntry { value, .. } => {
|
|
lines.push(" Enter the AWS Region to use with Amazon Bedrock.".into());
|
|
lines.push("".into());
|
|
lines.push(value.line(" AWS Region: ", "", FieldDisplay::Focused, area.width));
|
|
}
|
|
BedrockView::AccessKeyEntry {
|
|
values,
|
|
selected_field,
|
|
} => {
|
|
lines.push(" Enter your AWS access keys.".into());
|
|
lines.push("".into());
|
|
for (index, label) in [
|
|
"AWS access key ID",
|
|
"AWS secret access key",
|
|
"AWS session token (optional)",
|
|
]
|
|
.into_iter()
|
|
.enumerate()
|
|
{
|
|
let marker = if index == *selected_field { ">" } else { " " };
|
|
let display = match (index == *selected_field, index == 0) {
|
|
(true, true) => FieldDisplay::Focused,
|
|
(false, true) => FieldDisplay::Unfocused,
|
|
(true, false) => FieldDisplay::FocusedSecret,
|
|
(false, false) => FieldDisplay::UnfocusedSecret,
|
|
};
|
|
let line =
|
|
values[index].line(&format!("{marker} {label}: "), "", display, area.width);
|
|
lines.push(if index == *selected_field {
|
|
line.cyan()
|
|
} else {
|
|
line
|
|
});
|
|
}
|
|
}
|
|
BedrockView::EnvironmentInstructions => {
|
|
lines.push(
|
|
" Export AWS credentials, then run `codex --no-daemon` in that shell.".into(),
|
|
);
|
|
lines.push("".into());
|
|
lines.push(Line::from(vec![
|
|
" Setup guide: ".into(),
|
|
"https://learn.chatgpt.com/docs/amazon-bedrock"
|
|
.cyan()
|
|
.underlined(),
|
|
]));
|
|
lines.push("".into());
|
|
self.render_methods(&mut lines);
|
|
}
|
|
}
|
|
if !matches!(self.view, BedrockView::Discovering(_)) {
|
|
footer.push("".into());
|
|
if !matches!(self.view, BedrockView::Configuring(_)) {
|
|
footer.push(Line::from(vec![
|
|
" Press ".dim(),
|
|
keys::CONFIRM[0].into(),
|
|
" to continue".dim(),
|
|
]));
|
|
}
|
|
if !matches!(self.view, BedrockView::GovCloudWarning(_)) {
|
|
footer.push(Line::from(vec![
|
|
" Press ".dim(),
|
|
keys::CANCEL[0].into(),
|
|
" to go back".dim(),
|
|
]));
|
|
}
|
|
}
|
|
if let Some(error) = error {
|
|
footer.push("".into());
|
|
footer.push(error.red().into());
|
|
}
|
|
let mut lines = word_wrap_lines(lines, usize::from(area.width));
|
|
let mut footer = word_wrap_lines(footer, usize::from(area.width));
|
|
if matches!(self.view, BedrockView::GovCloudWarning(_))
|
|
&& lines.len() + footer.len() > usize::from(area.height)
|
|
{
|
|
footer = vec!["> Acknowledge".cyan().into()];
|
|
if area.height > 2 {
|
|
footer.push(Line::from(vec![
|
|
keys::MOVE_UP[0].into(),
|
|
"/".dim(),
|
|
keys::MOVE_DOWN[0].into(),
|
|
" scroll · ".dim(),
|
|
keys::CONFIRM[0].into(),
|
|
]));
|
|
}
|
|
footer = word_wrap_lines(footer, usize::from(area.width));
|
|
}
|
|
if lines.len() + footer.len() <= usize::from(area.height) || footer.is_empty() {
|
|
lines.extend(footer);
|
|
Paragraph::new(lines).render(area, buf);
|
|
if let BedrockView::GovCloudWarning(scroll) = &self.view {
|
|
scroll.store(0, Ordering::Relaxed);
|
|
crate::terminal_hyperlinks::mark_underlined_hyperlink(
|
|
buf,
|
|
area,
|
|
GOV_CLOUD_GUIDANCE_URL,
|
|
);
|
|
}
|
|
return;
|
|
}
|
|
let footer_height = u16::try_from(footer.len())
|
|
.unwrap_or(u16::MAX)
|
|
.min(area.height.saturating_sub(1));
|
|
let content_height = area.height.saturating_sub(footer_height);
|
|
let highlighted_row = lines
|
|
.iter()
|
|
.enumerate()
|
|
.skip(1)
|
|
.find_map(|(index, line)| {
|
|
line.spans
|
|
.first()
|
|
.is_some_and(|span| span.content.starts_with("> "))
|
|
.then_some(index)
|
|
})
|
|
.unwrap_or_default();
|
|
let max_scroll = lines.len().saturating_sub(usize::from(content_height));
|
|
let scroll = if let BedrockView::GovCloudWarning(scroll) = &self.view {
|
|
let offset = scroll.load(Ordering::Relaxed).min(max_scroll);
|
|
scroll.store(offset, Ordering::Relaxed);
|
|
offset
|
|
} else {
|
|
highlighted_row
|
|
.saturating_add(2)
|
|
.saturating_sub(usize::from(content_height))
|
|
.min(max_scroll)
|
|
};
|
|
let content_area = Rect {
|
|
height: content_height,
|
|
..area
|
|
};
|
|
let footer_area = Rect {
|
|
y: area.y.saturating_add(content_height),
|
|
height: footer_height,
|
|
..area
|
|
};
|
|
Paragraph::new(lines)
|
|
.scroll((u16::try_from(scroll).unwrap_or(u16::MAX), 0))
|
|
.render(content_area, buf);
|
|
Paragraph::new(footer).render(footer_area, buf);
|
|
if matches!(self.view, BedrockView::GovCloudWarning(_)) {
|
|
crate::terminal_hyperlinks::mark_underlined_hyperlink(
|
|
buf,
|
|
content_area,
|
|
GOV_CLOUD_GUIDANCE_URL,
|
|
);
|
|
}
|
|
}
|
|
|
|
fn render_methods(&self, lines: &mut Vec<Line<'static>>) {
|
|
for (index, method) in self.methods().into_iter().enumerate() {
|
|
let (title, description) = match method {
|
|
BedrockMethod::Profile(profile_index) => {
|
|
let profile = &self.profiles[profile_index];
|
|
let title = if self.profiles.len() == 1 {
|
|
format!("Continue with {}", profile.name)
|
|
} else {
|
|
profile.name.clone()
|
|
};
|
|
let description = if self.profiles.len() == 1 {
|
|
"Use your existing AWS credentials".to_string()
|
|
} else {
|
|
profile.region.clone().unwrap_or_default()
|
|
};
|
|
(title, description)
|
|
}
|
|
BedrockMethod::Environment => {
|
|
let description = if self.environment_credentials.iter().any(|credential| {
|
|
credential.credential_type == AwsCredentialType::BedrockApiKey
|
|
}) {
|
|
"Use the Bedrock API key from the server environment"
|
|
} else {
|
|
"Use AWS access keys from the server environment"
|
|
};
|
|
(
|
|
"Continue with detected credentials".to_string(),
|
|
description.to_string(),
|
|
)
|
|
}
|
|
BedrockMethod::OtherMethods => (
|
|
if matches!(self.view, BedrockView::EnvironmentInstructions) {
|
|
"Choose another sign-in method"
|
|
} else {
|
|
"Other AWS sign-in methods"
|
|
}
|
|
.to_string(),
|
|
if matches!(self.view, BedrockView::EnvironmentInstructions) {
|
|
""
|
|
} else {
|
|
"Use another profile, access keys, environment variables, or a Bedrock API key"
|
|
}
|
|
.to_string(),
|
|
),
|
|
BedrockMethod::ManualProfile => (
|
|
"AWS profile".to_string(),
|
|
"Use AWS SSO or a named profile".to_string(),
|
|
),
|
|
BedrockMethod::AccessKeys => (
|
|
"AWS access keys".to_string(),
|
|
"Enter an access key ID and secret access key".to_string(),
|
|
),
|
|
BedrockMethod::EnvironmentInstructions => (
|
|
"Environment variables".to_string(),
|
|
"Use AWS credentials exported in your shell".to_string(),
|
|
),
|
|
BedrockMethod::ApiKey => (
|
|
"Bedrock API key".to_string(),
|
|
"Enter a Bedrock API key".to_string(),
|
|
),
|
|
};
|
|
let selected = index == self.highlighted;
|
|
let marker = if selected { ">" } else { " " };
|
|
let title_line = format!("{marker} {}. {title}", index + 1);
|
|
lines.push(if selected {
|
|
title_line.cyan().into()
|
|
} else {
|
|
title_line.into()
|
|
});
|
|
if !description.is_empty()
|
|
&& (!matches!(self.view, BedrockView::Methods(BedrockMethodList::Detected))
|
|
|| !self.profiles.is_empty()
|
|
|| self.environment_credentials.is_empty())
|
|
{
|
|
lines.push(format!(" {description}").dim().into());
|
|
}
|
|
lines.push("".into());
|
|
}
|
|
}
|
|
}
|
|
|
|
pub(crate) async fn check_gov_cloud(request_handle: AppServerRequestHandle) -> bool {
|
|
match tokio::time::timeout(
|
|
std::time::Duration::from_secs(/*secs*/ 15),
|
|
request_handle.request_typed::<BedrockCheckGovCloudRequirementsResponse>(
|
|
ClientRequest::BedrockCheckGovCloudRequirements {
|
|
request_id: onboarding_request_id(),
|
|
params: BedrockCheckGovCloudRequirementsParams {},
|
|
},
|
|
),
|
|
)
|
|
.await
|
|
{
|
|
Ok(Ok(response)) => response.is_gov_cloud,
|
|
Ok(Err(_)) | Err(_) => false,
|
|
}
|
|
}
|
|
|
|
impl AuthModeWidget {
|
|
pub(super) fn start_bedrock_discovery(&mut self) {
|
|
let request_id = onboarding_request_id();
|
|
*self
|
|
.sign_in_state
|
|
.write()
|
|
.unwrap_or_else(PoisonError::into_inner) =
|
|
SignInState::Bedrock(BedrockState::discovering(request_id.clone()));
|
|
*self.error.write().unwrap_or_else(PoisonError::into_inner) = None;
|
|
self.request_frame.schedule_frame();
|
|
|
|
let request_handle = self.app_server_request_handle.clone();
|
|
let sign_in_state = self.sign_in_state.clone();
|
|
let error = self.error.clone();
|
|
let request_frame = self.request_frame.clone();
|
|
tokio::spawn(async move {
|
|
let result = request_handle
|
|
.request_typed::<BedrockDiscoverResponse>(ClientRequest::BedrockDiscover {
|
|
request_id: request_id.clone(),
|
|
params: BedrockDiscoverParams {},
|
|
})
|
|
.await;
|
|
let mut guard = sign_in_state
|
|
.write()
|
|
.unwrap_or_else(PoisonError::into_inner);
|
|
if !matches!(
|
|
&*guard,
|
|
SignInState::Bedrock(state)
|
|
if matches!(&state.view, BedrockView::Discovering(active_request_id)
|
|
if *active_request_id == request_id)
|
|
) {
|
|
return;
|
|
}
|
|
match result {
|
|
Ok(response) => {
|
|
*guard = SignInState::Bedrock(BedrockState::discovered(response));
|
|
}
|
|
Err(err) => {
|
|
*guard =
|
|
SignInState::Bedrock(BedrockState::discovered(BedrockDiscoverResponse {
|
|
profiles: Vec::new(),
|
|
environment_credentials: Vec::new(),
|
|
}));
|
|
*error.write().unwrap_or_else(PoisonError::into_inner) =
|
|
Some(format!("Unable to check AWS credentials: {err}"));
|
|
}
|
|
}
|
|
drop(guard);
|
|
request_frame.schedule_frame();
|
|
});
|
|
}
|
|
|
|
pub(super) fn handle_bedrock_key_event(&mut self, key_event: &KeyEvent) -> bool {
|
|
{
|
|
let mut guard = self
|
|
.sign_in_state
|
|
.write()
|
|
.unwrap_or_else(PoisonError::into_inner);
|
|
let SignInState::Bedrock(state) = &mut *guard else {
|
|
return false;
|
|
};
|
|
*self.error.write().unwrap_or_else(PoisonError::into_inner) = None;
|
|
match state.handle_key_event(key_event) {
|
|
Some(BedrockAction::BackToAuth) => {
|
|
*guard = SignInState::PickMode;
|
|
}
|
|
Some(BedrockAction::Configure(credential, region)) => {
|
|
let fallback = state.clone();
|
|
drop(guard);
|
|
self.start_bedrock_setup(credential, region, fallback);
|
|
}
|
|
None => {}
|
|
}
|
|
}
|
|
self.request_frame.schedule_frame();
|
|
true
|
|
}
|
|
|
|
fn start_bedrock_setup(
|
|
&mut self,
|
|
credential: BedrockCredential,
|
|
region: String,
|
|
mut fallback: BedrockState,
|
|
) {
|
|
fallback.enter_region(credential.clone(), ®ion);
|
|
let mut configuring = fallback.clone();
|
|
let request_id = onboarding_request_id();
|
|
configuring.view = BedrockView::Configuring(request_id.clone());
|
|
*self
|
|
.sign_in_state
|
|
.write()
|
|
.unwrap_or_else(PoisonError::into_inner) = SignInState::Bedrock(configuring);
|
|
|
|
let request_handle = self.app_server_request_handle.clone();
|
|
let active_request_id = request_id.clone();
|
|
let request = async move {
|
|
let result = match credential {
|
|
BedrockCredential::ApiKey(api_key) => request_handle
|
|
.request_typed::<LoginAccountResponse>(ClientRequest::LoginAccount {
|
|
request_id,
|
|
params: LoginAccountParams::AmazonBedrock {
|
|
api_key,
|
|
region,
|
|
provider_id: None,
|
|
},
|
|
})
|
|
.await
|
|
.map_err(|error| error.to_string())
|
|
.and_then(|response| match response {
|
|
LoginAccountResponse::AmazonBedrock {} => Ok(()),
|
|
response => Err(format!(
|
|
"Unexpected account/login/start response: {response:?}"
|
|
)),
|
|
}),
|
|
BedrockCredential::Profile(profile) => request_handle
|
|
.request_typed::<BedrockSetupResponse>(ClientRequest::BedrockSetup {
|
|
request_id,
|
|
params: BedrockSetupParams::Profile {
|
|
profile,
|
|
region,
|
|
provider_id: None,
|
|
},
|
|
})
|
|
.await
|
|
.map(|_| ())
|
|
.map_err(|error| error.to_string()),
|
|
BedrockCredential::Environment => request_handle
|
|
.request_typed::<BedrockSetupResponse>(ClientRequest::BedrockSetup {
|
|
request_id,
|
|
params: BedrockSetupParams::Environment {
|
|
region,
|
|
provider_id: None,
|
|
},
|
|
})
|
|
.await
|
|
.map(|_| ())
|
|
.map_err(|error| error.to_string()),
|
|
BedrockCredential::AccessKeys {
|
|
access_key_id,
|
|
secret_access_key,
|
|
session_token,
|
|
} => request_handle
|
|
.request_typed::<LoginAccountResponse>(ClientRequest::LoginAccount {
|
|
request_id,
|
|
params: LoginAccountParams::AmazonBedrockAccessKeys {
|
|
access_key_id,
|
|
secret_access_key,
|
|
session_token,
|
|
region,
|
|
provider_id: None,
|
|
},
|
|
})
|
|
.await
|
|
.map_err(|error| error.to_string())
|
|
.and_then(|response| match response {
|
|
LoginAccountResponse::AmazonBedrock {} => Ok(()),
|
|
response => Err(format!(
|
|
"Unexpected account/login/start response: {response:?}"
|
|
)),
|
|
}),
|
|
};
|
|
result.map_err(|err| format!("Unable to set up Amazon Bedrock: {err}"))
|
|
};
|
|
self.spawn_provider_setup(active_request_id, SignInState::Bedrock(fallback), request);
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[path = "bedrock_tests.rs"]
|
|
mod tests;
|