Skip to content

Commit

Permalink
feat: add support for configurable api keys with keyring
Browse files Browse the repository at this point in the history
  • Loading branch information
wolf4ood committed Sep 12, 2024
1 parent 7814f46 commit fda43fc
Show file tree
Hide file tree
Showing 6 changed files with 258 additions and 22 deletions.
166 changes: 158 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions edc-connector-tui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ edc-connector-client = { path = "../edc-connector-client", version = "0.1.0"}
enum-ordinalize = "4.3.0"
strum = "0.26.2"
arboard = { version = "3.4.0", features = ["wayland-data-control"] }
keyring = { version = "3", features = ["apple-native", "windows-native", "sync-secret-service"] }
46 changes: 36 additions & 10 deletions edc-connector-tui/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ pub mod model;
mod msg;

use crossterm::event::{self, Event, KeyCode};
use edc_connector_client::EdcConnectorClient;
use edc_connector_client::{Auth, EdcConnectorClient};
use keyring::Entry;
use ratatui::{
layout::{Constraint, Direction, Layout, Rect},
Frame,
Expand All @@ -18,9 +19,9 @@ use crate::{
header::HeaderComponent, launch_bar::LaunchBar, policies::PolicyDefinitionsComponent,
Component, ComponentEvent, ComponentMsg, ComponentReturn,
},
config::Config,
config::{AuthKind, Config, ConnectorConfig},
types::{
connector::Connector,
connector::{Connector, ConnectorStatus},
info::InfoSheet,
nav::{Menu, Nav},
},
Expand All @@ -41,17 +42,42 @@ pub struct App {
}

impl App {
fn auth(cfg: &ConnectorConfig) -> (ConnectorStatus, Auth) {
match cfg.auth() {
AuthKind::NoAuth => (ConnectorStatus::Connected, Auth::NoAuth),
AuthKind::Token { token_alias } => {
let entry =
Entry::new("edc-tui", &token_alias).and_then(|entry| entry.get_password());

match entry {
Ok(pwd) => (ConnectorStatus::Connected, Auth::api_token(pwd)),
Err(_err) => (
ConnectorStatus::Custom(format!(
"Token not found for alias {}",
token_alias
)),
Auth::NoAuth,
),
}
}
}
}

fn init_connector(cfg: ConnectorConfig) -> Connector {
let (status, auth) = Self::auth(&cfg);
let client = EdcConnectorClient::builder()
.management_url(cfg.address())
.with_auth(auth)
.build()
.unwrap();
Connector::new(cfg, client, status)
}

pub fn init(cfg: Config) -> App {
let connectors = cfg
.connectors
.into_iter()
.map(|cfg| {
let client = EdcConnectorClient::builder()
.management_url(cfg.address())
.build()
.unwrap();
Connector::new(cfg, client)
})
.map(App::init_connector)
.collect();
let connectors = ConnectorsComponent::new(connectors);

Expand Down
9 changes: 7 additions & 2 deletions edc-connector-tui/src/components/connectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@ pub struct ConnectorEntry(Connector);

impl TableEntry for ConnectorEntry {
fn row(&self) -> Row {
Row::new(vec![self.0.config().name(), self.0.config().address()])
Row::new(vec![
self.0.config().name(),
self.0.config().address(),
self.0.config().auth().kind(),
self.0.status().as_str(),
])
}

fn headers() -> Row<'static> {
Row::new(vec!["NAME", "ADDRESS"])
Row::new(vec!["NAME", "ADDRESS", "AUTH", "STATUS"])
}
}

Expand Down
26 changes: 26 additions & 0 deletions edc-connector-tui/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,28 @@ pub fn default_file() -> anyhow::Result<PathBuf> {
pub struct ConnectorConfig {
name: String,
address: String,
#[serde(default)]
auth: AuthKind,
}

#[derive(Deserialize, Debug, Clone, Default)]
#[serde(tag = "type")]
#[serde(rename_all = "kebab-case")]
pub enum AuthKind {
#[default]
NoAuth,
Token {
token_alias: String,
},
}

impl AuthKind {
pub fn kind(&self) -> &str {
match self {
AuthKind::NoAuth => "No auth",
AuthKind::Token { .. } => "Token based",
}
}
}

impl ConnectorConfig {
Expand All @@ -57,4 +79,8 @@ impl ConnectorConfig {
pub fn address(&self) -> &str {
&self.address
}

pub fn auth(&self) -> &AuthKind {
&self.auth
}
}
Loading

0 comments on commit fda43fc

Please sign in to comment.