| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | | use super::settings::{csrf, require}; |
| 6 | | use super::{filters, lookup, lookup_role, Error, Page, RepoShell, Shell}; |
| 7 | | use crate::auth::{Access, Viewer}; |
| 8 | | use crate::db::flow as fdb; |
| 9 | | use crate::{db, Shared}; |
| 10 | | use askama::Template; |
| 11 | | use axum::extract::{Form, Path, Query, State}; |
| 12 | | use axum::http::header; |
| 13 | | use axum::response::{IntoResponse, Response}; |
| 14 | | use serde::Deserialize; |
| 15 | |
|
| 16 | |
|
| 17 | | const ARTIFACTS_SHOWN: i64 = 200; |
| 18 | |
|
| 19 | | const IMAGE_DAYS: i64 = 90; |
| 20 | |
|
| 21 | | #[derive(Template)] |
| 22 | | #[template(path = "repo_artifacts.html")] |
| 23 | | pub struct ArtifactsPage { |
| 24 | | shell: Shell, |
| 25 | | repo: RepoShell, |
| 26 | | artifacts: Vec<fdb::RepoArtifact>, |
| 27 | | limit: i64, |
| 28 | | } |
| 29 | |
|
| 30 | |
|
| 31 | 2× | pub async fn artifacts( |
| 32 | 2× | State(app): State<Shared>, |
| 33 | 2× | viewer: Viewer, |
| 34 | 2× | Path((owner, name)): Path<(String, String)>, |
| 35 | 2× | ) -> Result<Page<ArtifactsPage>, Error> { |
| 36 | 2× | let shell = Shell::new(&app, &format!("/{owner}/{name}/artifacts"), &viewer); |
| 37 | 2× | let (_repo, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 38 | 2× | let artifacts = fdb::repo_artifacts(&app.db, row.id, ARTIFACTS_SHOWN).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 39 | 2× | Ok(Page(ArtifactsPage { shell, repo: RepoShell::new(&app, &row, "artifacts").with_role(role), artifacts, limit: ARTIFACTS_SHOWN })) |
| 40 | 2× | } |
| 41 | |
|
| 42 | |
|
| 43 | | pub struct ImageView { |
| 44 | | |
| 45 | | pub image: String, |
| 46 | | pub hashed: bool, |
| 47 | | pub jobs: i64, |
| 48 | | pub last_ms: i64, |
| 49 | | |
| 50 | | pub kept: Option<(i64, String)>, |
| 51 | | } |
| 52 | |
|
| 53 | | #[derive(Template)] |
| 54 | | #[template(path = "repo_images.html")] |
| 55 | | pub struct ImagesPage { |
| 56 | | shell: Shell, |
| 57 | | repo: RepoShell, |
| 58 | | images: Vec<ImageView>, |
| 59 | | days: i64, |
| 60 | | } |
| 61 | |
|
| 62 | |
|
| 63 | 1× | pub async fn images( |
| 64 | 1× | State(app): State<Shared>, |
| 65 | 1× | viewer: Viewer, |
| 66 | 1× | Path((owner, name)): Path<(String, String)>, |
| 67 | 1× | ) -> Result<Page<ImagesPage>, Error> { |
| 68 | 1× | let shell = Shell::new(&app, &format!("/{owner}/{name}/images"), &viewer); |
| 69 | 1× | let (_repo, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 70 | 1× | let e = |e| Error::from_db(shell.clone(), e); |
| 71 | 1× | let uses = crate::images::uses_of(&app.db, row.id, db::now_ms() - IMAGE_DAYS * 24 * 3600 * 1000).await.map_err(e)?; |
| 72 | 1× | let kept = crate::images::kept(&app.db).await.map_err(e)?; |
| 73 | 1× | let images = uses |
| 74 | 1× | .into_iter() |
| 75 | 1× | .map(|u| { |
| 76 | 0× | let kept = kept.iter().find(|k| k.hash.eq_ignore_ascii_case(&u.image)).map(|k| (k.size, k.arch.clone())); |
| 77 | 0× | ImageView { hashed: jet::images::is_hash(&u.image), image: u.image, jobs: u.jobs, last_ms: u.last_ms, kept } |
| 78 | 0× | }) |
| 79 | 1× | .collect(); |
| 80 | 1× | Ok(Page(ImagesPage { shell, repo: RepoShell::new(&app, &row, "images").with_role(role), images, days: IMAGE_DAYS })) |
| 81 | 1× | } |
| 82 | |
|
| 83 | |
|
| 84 | | pub struct StateView { |
| 85 | | pub s: db::terraform::TfState, |
| 86 | | pub versions: Vec<db::terraform::TfVersion>, |
| 87 | | } |
| 88 | |
|
| 89 | | #[derive(Template)] |
| 90 | | #[template(path = "repo_terraform.html")] |
| 91 | | pub struct TerraformPage { |
| 92 | | shell: Shell, |
| 93 | | repo: RepoShell, |
| 94 | | states: Vec<StateView>, |
| 95 | | notice: Option<String>, |
| 96 | | error: Option<String>, |
| 97 | | } |
| 98 | |
|
| 99 | 5× | async fn terraform_page( |
| 100 | 5× | app: &Shared, |
| 101 | 5× | shell: Shell, |
| 102 | 5× | row: db::repos::RepoRow, |
| 103 | 5× | role: crate::auth::Role, |
| 104 | 5× | notice: Option<String>, |
| 105 | 5× | error: Option<String>, |
| 106 | 5× | ) -> Result<Page<TerraformPage>, Error> { |
| 107 | 5× | let e = |e| Error::from_db(shell.clone(), e); |
| 108 | 5× | let mut states = Vec::new(); |
| 109 | | |
| 110 | 5× | if crate::auth::roles::allows(Some(role), Access::Write) { |
| 111 | 3× | for s in db::terraform::of_repo(&app.db, row.id).await.map_err(e)? { |
| 112 | 3× | let versions = db::terraform::version_list(&app.db, row.id, &s.name).await.map_err(e)?; |
| 113 | 3× | states.push(StateView { s, versions }); |
| 114 | | } |
| 115 | 2× | } |
| 116 | 5× | Ok(Page(TerraformPage { shell, repo: RepoShell::new(app, &row, "terraform").with_role(role), states, notice, error })) |
| 117 | 5× | } |
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | 4× | pub async fn terraform( |
| 122 | 4× | State(app): State<Shared>, |
| 123 | 4× | viewer: Viewer, |
| 124 | 4× | Path((owner, name)): Path<(String, String)>, |
| 125 | 4× | ) -> Result<Page<TerraformPage>, Error> { |
| 126 | 4× | let shell = Shell::new(&app, &format!("/{owner}/{name}/terraform"), &viewer); |
| 127 | 4× | let (_repo, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 128 | 4× | terraform_page(&app, shell, row, role, None, None).await |
| 129 | 4× | } |
| 130 | |
|
| 131 | | #[derive(Deserialize)] |
| 132 | | pub struct DownloadQuery { |
| 133 | | version: Option<i64>, |
| 134 | | } |
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | 1× | pub async fn download( |
| 139 | 1× | State(app): State<Shared>, |
| 140 | 1× | viewer: Viewer, |
| 141 | 1× | Path((owner, name, state)): Path<(String, String, String)>, |
| 142 | 1× | Query(q): Query<DownloadQuery>, |
| 143 | 1× | ) -> Result<Response, Error> { |
| 144 | 1× | let shell = Shell::new(&app, &format!("/{owner}/{name}/terraform"), &viewer); |
| 145 | 1× | let (_repo, row, _role) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Write).await?; |
| 146 | 1× | let e = |e| Error::from_db(shell.clone(), e); |
| 147 | 1× | let (body, serial) = match q.version { |
| 148 | 1× | Some(id) => db::terraform::version_body(&app.db, row.id, &state, id).await.map_err(e)?, |
| 149 | 0× | None => db::terraform::get(&app.db, row.id, &state).await.map_err(e)?.filter(|s| !s.body.is_empty()).map(|s| (s.body, s.serial)), |
| 150 | | } |
| 151 | 1× | .ok_or_else(|| Error::not_found(shell.clone()))?; |
| 152 | 1× | let plain = app.secret_key.open(&body).ok_or_else(|| Error::not_found(shell.clone()))?; |
| 153 | 4× | let file = format!("{}-serial-{serial}.tfstate", state.replace(|c: char| !(c.is_ascii_alphanumeric() || "-_.".contains(c)), "_")); |
| 154 | 1× | Ok(( |
| 155 | 1× | [ |
| 156 | 1× | (header::CONTENT_TYPE, "application/json".to_string()), |
| 157 | 1× | (header::CONTENT_DISPOSITION, format!("attachment; filename=\"{file}\"")), |
| 158 | 1× | (header::CACHE_CONTROL, "no-store".to_string()), |
| 159 | 1× | ], |
| 160 | 1× | plain, |
| 161 | 1× | ) |
| 162 | 1× | .into_response()) |
| 163 | 1× | } |
| 164 | |
|
| 165 | | #[derive(Deserialize)] |
| 166 | | pub struct RestoreForm { |
| 167 | | #[serde(rename = "_csrf")] |
| 168 | | csrf_token: String, |
| 169 | | version: i64, |
| 170 | | } |
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | 1× | pub async fn restore( |
| 175 | 1× | State(app): State<Shared>, |
| 176 | 1× | viewer: Viewer, |
| 177 | 1× | Path((owner, name, state)): Path<(String, String, String)>, |
| 178 | 1× | Form(f): Form<RestoreForm>, |
| 179 | 1× | ) -> Result<Page<TerraformPage>, Error> { |
| 180 | 1× | let shell = Shell::new(&app, &format!("/{owner}/{name}/terraform"), &viewer); |
| 181 | 1× | let me = require(&viewer, &shell)?; |
| 182 | 1× | let (_repo, row, role) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Maintain).await?; |
| 183 | 1× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 184 | 1× | match crate::terraform::restore(&app, row.id, &state, f.version, &me.account.username).await { |
| 185 | 1× | Ok(serial) => { |
| 186 | 1× | db::accounts::audit( |
| 187 | 1× | &app.db, |
| 188 | 1× | Some(&me.account), |
| 189 | 1× | "terraform.restored", |
| 190 | 1× | &format!("{owner}/{name}:{state}"), |
| 191 | 1× | &format!("version {}, now serial {serial}", f.version), |
| 192 | 1× | ) |
| 193 | 1× | .await |
| 194 | 1× | .ok(); |
| 195 | 1× | let msg = format!( |
| 196 | | "Restored {state}: it is now serial {serial}. The state it replaced is kept as a version, so you can go back to it." |
| 197 | | ); |
| 198 | 1× | terraform_page(&app, shell, row, role, Some(msg), None).await |
| 199 | | } |
| 200 | 0× | Err(e) => terraform_page(&app, shell, row, role, None, Some(format!("Could not restore {state}: {}.", e.message()))).await, |
| 201 | | } |
| 202 | 1× | } |