Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/self-host/check-your-install.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ Install admins (`admins` in the config) who are team admins can call these while
}
```

`status` is `pass`, `warn`, `fail` or `skip` (an optional integration that isn't set up). `fix` has `text` and any of `command` (to copy), `url` (outside CoderHelm) and `href` (a dashboard page). `ready` is true when nothing fails. Keys: `url`, `domain`, `github_keys`, `github_app`, `github_permissions`, `github_installations`, `github_webhook`, `email_sender`, `email_identity`, `email_sandbox`, `email_cognito`, `model`, `dlq`, `updates`, `slack`, `teams`, `jira`, `aws_connections`. A self-hosted install has no Google sign-in, so there is no `google_sign_in` check. On an install that uses CoderHelm's GitHub App (hybrid), `github_app` and `github_installations` say whether the app is linked instead, and never block an update.
`status` is `pass`, `warn`, `fail` or `skip` (an optional integration that isn't set up). `fix` has `text` and any of `command` (to copy), `url` (outside CoderHelm) and `href` (a dashboard page). `ready` is true when nothing fails. Keys: `url`, `domain`, `github_keys`, `github_app`, `github_permissions`, `github_installations`, `github_webhook`, `github_sign_in`, `email_sender`, `email_identity`, `email_sandbox`, `email_cognito`, `model`, `dlq`, `updates`, `slack`, `teams`, `jira`, `aws_connections`. A self-hosted install has no Google sign-in, so there is no `google_sign_in` check. On an install that uses CoderHelm's GitHub App (hybrid), `github_app` and `github_installations` say whether the app is linked instead, and never block an update. `github_sign_in` warns when the install's own GitHub App has no OAuth client id and secret stored (or only one of them): without both, the sign-in page doesn't offer Continue with GitHub. On an install that uses CoderHelm's GitHub App it is `skip`, since CoderHelm's app can't sign anyone in to the install; people sign in with email and password. It never blocks an update.

### `GET /api/system/domain`, `PUT /api/system/domain`, `DELETE /api/system/domain`

Expand Down
4 changes: 2 additions & 2 deletions docs/self-host/sign-in.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ An install that had Google sign-in on loses it at its next update, which removes

## GitHub accounts

People connect their GitHub account in their own settings, so pull requests and reviews are credited to them. Signing in with GitHub follows the same rules as everything else: it lands on the organization only for someone already in it, invited, or at an allowed domain (by the email on their GitHub account). It uses the GitHub App's own OAuth client, which the GitHub App step stores; nothing to set up.
People connect their GitHub account in their own settings, so pull requests and reviews are credited to them. Signing in with GitHub follows the same rules as everything else: it lands on the organization only for someone already in it, invited, or at an allowed domain (by the email on their GitHub account). It uses the GitHub App's own OAuth client, which the GitHub App step stores; nothing to set up. The sign-in page offers **Continue with GitHub** only while that client's id and secret are both stored (it asks the API, `GET /auth/methods`). An install on CoderHelm's GitHub App (hybrid) has no client of its own, so its sign-in page shows email and password only: signing in through CoderHelm would hand your people's identities to CoderHelm. The `github_sign_in` check in **Settings, Setup** says which applies.

## Single sign-on (SAML, OIDC)

Not available yet: CoderHelm's sign-in page offers email and password, and GitHub. The Cognito user pool is in your account, so an identity provider added there by hand is not shown on the sign-in page and isn't supported.
Not available yet: CoderHelm's sign-in page offers email and password, and GitHub when the install has its own GitHub App. The Cognito user pool is in your account, so an identity provider added there by hand is not shown on the sign-in page and isn't supported.

## Install admins and team roles

Expand Down
3 changes: 3 additions & 0 deletions services/gateway/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1433,6 +1433,9 @@ async fn main() -> Result<(), Error> {
"/integrations/jira/forge-register",
post(routes::api::forge_register_urls),
)
// The sign-in methods that work here: the sign-in page reads it on
// every load, so it stays out of the sign-in attempts' rate limit.
.route("/auth/methods", get(routes::auth::methods))
.nest("/auth", auth_routes)
.nest("/email", email_routes)
.nest("/agent-approvals", approval_link_routes)
Expand Down
31 changes: 31 additions & 0 deletions services/gateway/src/routes/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1234,8 +1234,36 @@ async fn google_callback_inner(

// ── GitHub OAuth (connect GitHub, not identity) ──────────────────────

/// GET /auth/methods: the sign-in methods that work on this deployment
/// (public, no session). The sign-in page shows these and no others.
pub async fn methods(State(state): State<Arc<AppState>>) -> Response {
let m = oauth_signin::sign_in_methods(super::setup_checks::self_hosted(), github_ready(&state));
Json(m).into_response()
}

fn github_ready(state: &AppState) -> bool {
oauth_signin::github_sign_in_ready(
&state.secrets.github_client_id,
&state.secrets.github_client_secret,
)
}

/// Without an OAuth client of the install's own, the trip to GitHub would
/// end on GitHub's 404: back to the sign-in screen with a plain sentence.
fn github_not_set_up(state: &AppState, step: &'static str) -> Option<Response> {
oauth_signin::github_not_set_up(
&state.secrets.github_client_id,
&state.secrets.github_client_secret,
common::public_urls::app_url(&state.config.stage),
step,
)
}

/// GET /auth/github — Redirect to GitHub OAuth (for connecting GitHub account).
pub async fn github_login(State(state): State<Arc<AppState>>) -> Response {
if let Some(r) = github_not_set_up(&state, "start") {
return r;
}
let client_id = &state.secrets.github_client_id;
let redirect_uri = format!(
"{}/auth/github/callback",
Expand Down Expand Up @@ -1267,6 +1295,9 @@ pub async fn github_callback(
headers: HeaderMap,
Query(params): Query<CallbackQuery>,
) -> Response {
if let Some(r) = github_not_set_up(&state, "callback") {
return r;
}
let app = common::public_urls::app_url(&state.config.stage);
match github_callback_inner(&state, &ip.0, &headers, &params).await {
Ok(resp) => resp,
Expand Down
90 changes: 90 additions & 0 deletions services/gateway/src/routes/oauth_signin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ pub enum Reason {
Provider,
/// Something on our side failed.
Server,
/// This install has no sign-in client for the provider, so the trip to
/// the provider would only end on its error page.
NotSetUp,
}

impl Reason {
Expand All @@ -66,6 +69,7 @@ impl Reason {
Reason::Cancelled => "cancelled",
Reason::Provider => "provider",
Reason::Server => "server",
Reason::NotSetUp => "not_set_up",
}
}
}
Expand Down Expand Up @@ -212,6 +216,51 @@ fn cookie_value<'a>(cookie_header: &'a str, name: &str) -> Option<&'a str> {
.filter(|v| !v.is_empty())
}

/// GitHub sign-in works only with an OAuth client of the install's own: its
/// client id and secret. A hybrid install on CoderHelm's GitHub App has
/// neither, and CoderHelm's client can't sign anyone in to it (the hosted
/// side never vouches for a person on an install).
pub fn github_sign_in_ready(client_id: &str, client_secret: &str) -> bool {
!client_id.trim().is_empty() && !client_secret.trim().is_empty()
}

/// `GET /auth/github` (and its callback) with no client: back to the sign-in
/// screen with `not_set_up`, never to GitHub with an empty `client_id`.
pub fn github_not_set_up(
client_id: &str,
client_secret: &str,
app_url: &str,
step: &'static str,
) -> Option<Response> {
(!github_sign_in_ready(client_id, client_secret)).then(|| {
SignInFailure::new(
Provider::Github,
Reason::NotSetUp,
step,
"no GitHub OAuth client id and secret stored for this install",
)
.into_redirect(app_url)
})
}

/// The sign-in methods that work here, which the sign-in page shows and
/// nothing else: email and password always, Google only on the hosted
/// deployment, GitHub only with the install's own OAuth client.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)]
pub struct SignInMethods {
pub password: bool,
pub google: bool,
pub github: bool,
}

pub fn sign_in_methods(self_hosted: bool, github_ready: bool) -> SignInMethods {
SignInMethods {
password: true,
google: !self_hosted,
github: github_ready,
}
}

/// An app's built-in browser (a web view inside another app), where Google
/// shows "This browser or app may not be secure" instead of a sign-in page.
///
Expand Down Expand Up @@ -259,6 +308,47 @@ mod tests {
}
}

#[test]
fn github_sign_in_needs_both_halves_of_the_client() {
assert!(github_sign_in_ready("Iv1.abc", "secret"));
// A hybrid install on CoderHelm's app: no client of its own.
assert!(!github_sign_in_ready("", ""));
assert!(!github_sign_in_ready("Iv1.abc", ""));
assert!(!github_sign_in_ready("", "secret"));
assert!(!github_sign_in_ready(" ", " \n"));
}

#[test]
fn the_methods_list_has_only_what_works() {
let m = sign_in_methods(true, false);
assert_eq!((m.password, m.google, m.github), (true, false, false));
let m = sign_in_methods(true, true);
assert_eq!((m.password, m.google, m.github), (true, false, true));
let m = sign_in_methods(false, true);
assert_eq!((m.password, m.google, m.github), (true, true, true));
assert_eq!(
serde_json::to_value(sign_in_methods(true, false)).unwrap(),
serde_json::json!({ "password": true, "google": false, "github": false })
);
}

#[test]
fn github_with_no_client_goes_back_to_sign_in_not_to_github() {
let app = "https://coderhelm.acme.example";
for (id, secret) in [("", ""), ("Iv1.abc", ""), ("", "secret")] {
let r = github_not_set_up(id, secret, app, "start").expect("refused");
assert_eq!(r.status(), StatusCode::FOUND);
let to = r.headers()[header::LOCATION].to_str().unwrap();
assert_eq!(
to,
"https://coderhelm.acme.example/?signin_error=not_set_up&provider=github"
);
assert!(!to.contains("github.com"), "{to}");
}
// With the install's own client, sign-in goes on to GitHub.
assert!(github_not_set_up("Iv1.abc", "secret", app, "start").is_none());
}

#[test]
fn a_matching_state_returns_the_code() {
let query = q(Some("abc"), Some("S1"), None);
Expand Down
107 changes: 105 additions & 2 deletions services/gateway/src/routes/setup_checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,59 @@ pub fn github_keys(app_id: &str, private_key: &str) -> Check {
)
}

/// GitHub sign-in: the sign-in page offers "Continue with GitHub" only with
/// an OAuth client of the install's own (its id and secret). `hosted_app` is
/// a hybrid install on CoderHelm's GitHub App, which has no client to store:
/// CoderHelm's can't sign anyone in to the install, so sign-in there is
/// email and password, and that's expected.
pub fn github_sign_in(client_id: &str, client_secret: &str, hosted_app: bool) -> Check {
const KEY: &str = "github_sign_in";
const LABEL: &str = "GitHub sign-in";
let (id, secret) = (
!client_id.trim().is_empty(),
!client_secret.trim().is_empty(),
);
if id && secret {
return Check::pass(
KEY,
G,
LABEL,
"The OAuth client is stored, so the sign-in page offers Continue with GitHub.",
);
}
if hosted_app && !id && !secret {
return Check::not_ok(
Status::Skip,
KEY,
G,
LABEL,
"Off: this install uses CoderHelm's GitHub App, which can't sign people in to this install.",
"People sign in with email and password; the sign-in page doesn't offer GitHub.",
Fix::text("Nothing to do. To offer GitHub sign-in, create a GitHub App of this install's own.")
.command(format!("{CLI} github-app")),
);
}
let detail = if id || secret {
format!(
"Only the OAuth client {} is stored; the {} is missing.",
if id { "id" } else { "secret" },
if id { "secret" } else { "id" }
)
} else {
"The GitHub App's OAuth client id and secret aren't stored.".to_string()
};
Check::not_ok(
Status::Warn,
KEY,
G,
LABEL,
detail,
"The sign-in page hides Continue with GitHub until both are stored, and connecting a GitHub account doesn't work.",
Fix::text("Generate a new client secret on the app's page and store the app again.")
.command(format!("{CLI} github-app --force")),
)
}

/// GitHub's answer to `GET /app` signed with the stored id and key.
pub fn github_app(status: Option<u16>) -> Check {
match status {
Expand Down Expand Up @@ -1928,11 +1981,19 @@ pub async fn run_all(state: &AppState, team_id: &str) -> (Vec<Check>, Option<Val
// Hybrid with CoderHelm's GitHub App: nothing of the install's
// own to check (its absence used to fail github_keys and block
// updates).
if super::hybrid_install::github_hosted(state).await {
let hosted = super::hybrid_install::github_hosted(state).await;
let mut out = if hosted {
github_hosted_checks(super::hybrid_install::linked_installations(state).await)
} else {
github_checks(state, true).await
}
};
let s = &state.secrets;
out.push(github_sign_in(
&s.github_client_id,
&s.github_client_secret,
hosted,
));
out
},
async { super::email_metrics::checks(&super::email_metrics::facts_now(state).await) },
model_check(state, team_id),
Expand Down Expand Up @@ -2538,6 +2599,48 @@ pub async fn status(

#[cfg(test)]
mod tests {
#[test]
fn github_sign_in_flags_a_missing_client_and_never_blocks_an_update() {
use super::{github_sign_in, preflight, Status};
let ok = github_sign_in("Iv1.abc", "secret", false);
assert_eq!((ok.key, ok.status), ("github_sign_in", Status::Pass));
// An install's own app with no client stored: GitHub sign-in can't work.
let none = github_sign_in("", "", false);
assert_eq!(none.status, Status::Warn);
assert!(none.detail.contains("aren't stored"), "{}", none.detail);
assert!(none
.fix
.as_ref()
.unwrap()
.command
.as_deref()
.unwrap()
.ends_with("github-app --force"));
// Half a client is as broken as none, on either kind of install.
for hosted in [false, true] {
let half = github_sign_in("Iv1.abc", " ", hosted);
assert_eq!(half.status, Status::Warn);
assert!(
half.detail.contains("the secret is missing"),
"{}",
half.detail
);
let half = github_sign_in("", "secret", hosted);
assert!(half.detail.contains("the id is missing"), "{}", half.detail);
}
// Hybrid on CoderHelm's app: no client is expected, sign-in is email and password.
let hybrid = github_sign_in("", "", true);
assert_eq!(hybrid.status, Status::Skip);
assert!(
hybrid.detail.contains("CoderHelm's GitHub App"),
"{}",
hybrid.detail
);
// A sign-in method is never a reason to hold back an update.
let blockers = preflight(&[none, hybrid], Some(&serde_json::json!({})));
assert!(blockers.iter().all(|b| b.key != "github_sign_in"));
}

#[test]
fn aws_connections_need_the_install_account_and_this_release_templates() {
use super::{aws_connections, Fetched, Status};
Expand Down
Loading