Skip to content

Commit 4cb94be

Browse files
authored
Merge pull request #6 from fiji-flo/cache
Global cache, save nicks hach, update deps.
2 parents 28e46b0 + 77aa0bc commit 4cb94be

7 files changed

Lines changed: 643 additions & 519 deletions

File tree

Cargo.lock

Lines changed: 578 additions & 505 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ cis_profile = { git = "https://github.com/fiji-flo/cis_profile-rust", branch = "
1414
oauth2 = "2.0.0-beta.2"
1515
url = "1.7.2"
1616
base64 = "0.10.1"
17-
rand = "0.6.5"
17+
rand = "0.7"
1818
actix-web = { version = "1.0", features = ["ssl"] }
1919
actix-cors = "0.1"
20-
actix-session = "0.1"
20+
actix-session = "0.2"
2121
failure = "0.1.5"
2222
config = "0.9.3"
2323
serde = "1.0.80"

ci/install_deps.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export RUSTUP_HOME=/usr/local/rustup
22
export CARGO_HOME=/usr/local/cargo
33
export PATH=/usr/local/cargo/bin:$PATH
4-
export RUST_VERSION=1.35.0
4+
export RUST_VERSION=1.36.0
55

66

77
set -eux

src/bugzilla/app.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ pub struct Auth {
4141
state: String,
4242
}
4343

44-
#[derive(Deserialize)]
44+
#[derive(Deserialize, Debug)]
4545
pub struct BugZillaUser {
4646
id: String,
4747
login: String,
48+
nick: Option<String>,
4849
}
4950

5051
fn redirect(client: web::Data<Arc<BasicClient>>, session: Session) -> impl Responder {
@@ -102,10 +103,15 @@ fn auth<T: AsyncCisClientTrait + 'static>(
102103
res.json::<BugZillaUser>().map_err(Into::into)
103104
})
104105
.and_then(move |j| {
105-
info!("login: {}, id: {}", j.login, j.id);
106+
info!(
107+
"login: {}, id: {}, nick: {}",
108+
j.login,
109+
j.id,
110+
j.nick.as_ref().map(|s| s.as_str()).unwrap_or_default()
111+
);
106112
get.get_user_by(&get_uid, &GetBy::UserId, None)
107113
.and_then(move |profile: Profile| {
108-
update_bugzilla(j.id, j.login, profile, get.get_secret_store())
114+
update_bugzilla(j.id, j.login, j.nick, profile, get.get_secret_store())
109115
.into_future()
110116
.map_err(Into::into)
111117
})

src/github/app.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ fn auth<T: AsyncCisClientTrait + 'static>(
160160
j.node_id,
161161
format!("{}", j.id),
162162
j.email,
163+
j.login,
163164
profile,
164165
get.get_secret_store(),
165166
)
@@ -191,13 +192,13 @@ pub fn github_app<T: AsyncCisClientTrait + 'static>(
191192
github: &GitHub,
192193
whoami: &WhoAmI,
193194
secret: &[u8],
195+
ttl_cache: Arc<RwLock<TtlCache<String, String>>>,
194196
cis_client: T,
195197
) -> impl HttpServiceFactory {
196198
let github_client_id = ClientId::new(github.client_id.clone());
197199
let github_client_secret = ClientSecret::new(github.client_secret.clone());
198200
let auth_url = AuthUrl::new(Url::parse(AUTH_URL).expect("Invalid authorization endpoint URL"));
199201
let token_url = TokenUrl::new(Url::parse(TOKEN_URL).expect("Invalid token endpoint URL"));
200-
let ttl_cache = Arc::new(RwLock::new(TtlCache::<String, String>::new(2000)));
201202

202203
let client = Arc::new(
203204
BasicClient::new(

src/main.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ use crate::github::app::github_app;
2828
use actix_web::middleware::Logger;
2929
use actix_web::web;
3030
use actix_web::App;
31+
use std::sync::Arc;
32+
use std::sync::RwLock;
33+
use ttl_cache::TtlCache;
3134

3235
use actix_web::HttpServer;
3336
use failure::Error;
@@ -40,6 +43,7 @@ fn main() -> Result<(), Error> {
4043
let client = cis_client::CisClient::from_settings(&s.cis)?;
4144
info!("initialized cis_client");
4245
let secret = base64::decode(&s.whoami.secret)?;
46+
let ttl_cache = Arc::new(RwLock::new(TtlCache::<String, String>::new(2000)));
4347
HttpServer::new(move || {
4448
App::new()
4549
.wrap(Logger::default().exclude("/healthz"))
@@ -49,6 +53,7 @@ fn main() -> Result<(), Error> {
4953
&s.providers.github,
5054
&s.whoami,
5155
&secret,
56+
Arc::clone(&ttl_cache),
5257
client.clone(),
5358
))
5459
.service(bugzilla_app(

src/update.rs

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,46 @@ use chrono::Utc;
33
use cis_profile::crypto::SecretStore;
44
use cis_profile::crypto::Signer;
55
use cis_profile::schema::Display;
6+
use cis_profile::schema::KeyValue;
67
use cis_profile::schema::Profile;
78
use cis_profile::schema::PublisherAuthority;
89
use cis_profile::schema::StandardAttributeString;
10+
use cis_profile::schema::StandardAttributeValues;
911
use failure::Error;
1012

13+
fn create_usernames_key(typ: &str) -> String {
14+
format!("HACK#{}", typ)
15+
}
16+
1117
pub fn update_github(
1218
github_v4_id: String,
1319
github_v3_id: String,
1420
github_email: Option<String>,
21+
github_login: String,
1522
mut profile: Profile,
1623
store: &SecretStore,
1724
) -> Result<Profile, Error> {
1825
let now = &Utc::now().to_rfc3339_opts(SecondsFormat::Secs, true);
19-
update_and_sign(
26+
update_and_sign_string_field(
2027
&mut profile.identities.github_id_v3,
2128
github_v3_id,
2229
store,
2330
&now,
2431
)?;
25-
update_and_sign(
32+
update_and_sign_string_field(
2633
&mut profile.identities.github_id_v4,
2734
github_v4_id,
2835
store,
2936
&now,
3037
)?;
38+
update_and_sign_values_field(
39+
&mut profile.usernames,
40+
vec![(create_usernames_key("GITHUB"), github_login)],
41+
store,
42+
&now,
43+
)?;
3144
if let Some(email) = github_email {
32-
update_and_sign(
45+
update_and_sign_string_field(
3346
&mut profile.identities.github_primary_email,
3447
email,
3548
store,
@@ -42,26 +55,52 @@ pub fn update_github(
4255
pub fn update_bugzilla(
4356
bugzilla_id: String,
4457
bugzilla_email: String,
58+
bugzilla_nick: Option<String>,
4559
mut profile: Profile,
4660
store: &SecretStore,
4761
) -> Result<Profile, Error> {
4862
let now = &Utc::now().to_rfc3339_opts(SecondsFormat::Secs, true);
49-
update_and_sign(
63+
update_and_sign_string_field(
5064
&mut profile.identities.bugzilla_mozilla_org_id,
5165
bugzilla_id,
5266
store,
5367
&now,
5468
)?;
55-
update_and_sign(
69+
update_and_sign_string_field(
5670
&mut profile.identities.bugzilla_mozilla_org_primary_email,
57-
bugzilla_email,
71+
bugzilla_email.clone(),
5872
store,
5973
&now,
6074
)?;
75+
76+
let mut kv_pairs = vec![(create_usernames_key("BMOMAIL"), bugzilla_email)];
77+
if let Some(nick) = bugzilla_nick {
78+
kv_pairs.push((create_usernames_key("BMONICK"), nick));
79+
}
80+
update_and_sign_values_field(&mut profile.usernames, kv_pairs, store, &now)?;
6181
Ok(profile)
6282
}
6383

64-
fn update_and_sign(
84+
fn update_and_sign_values_field(
85+
field: &mut StandardAttributeValues,
86+
kv_pairs: Vec<(String, String)>,
87+
store: &SecretStore,
88+
now: &str,
89+
) -> Result<(), Error> {
90+
if let Some(KeyValue(ref mut values)) = &mut field.values {
91+
for (k, v) in kv_pairs.into_iter() {
92+
values.insert(k, Some(v));
93+
}
94+
}
95+
if field.metadata.display.is_none() {
96+
field.metadata.display = Some(Display::Staff);
97+
}
98+
field.metadata.last_modified = now.to_owned();
99+
field.signature.publisher.name = PublisherAuthority::Mozilliansorg;
100+
store.sign_attribute(field)
101+
}
102+
103+
fn update_and_sign_string_field(
65104
field: &mut StandardAttributeString,
66105
value: String,
67106
store: &SecretStore,

0 commit comments

Comments
 (0)