From 686647fbdd6b3f6d03983ab4663616dd0e8cf89c Mon Sep 17 00:00:00 2001 From: NickJ-7010 Date: Sat, 10 Oct 2026 03:18:29 -0400 Subject: [PATCH 1/9] fix: Initial commit for bootstrap 5 upgrade --- .env.example | 30 +++++++++ HACKING/README.md | 3 - HACKING/dev_server.sh | 7 -- HACKING/launch_environment.sh | 7 -- HACKING/sample.env | 26 -------- README.md | 73 ++++++++++++++++++++- docker-compose.yaml | 44 +++++++++++++ env-template | 33 ---------- ldap.go | 5 ++ main.go | 17 +++-- routes.go | 13 ++++ s3.go | 12 ++-- static/plug.css | 26 -------- templates/header.tmpl | 118 ++++++++++++++++++++++++++++++++++ templates/success.tmpl | 47 ++++---------- templates/upload.tmpl | 107 +++++++++++++----------------- templates/view_plugs.tmpl | 48 ++++---------- 17 files changed, 367 insertions(+), 249 deletions(-) create mode 100644 .env.example delete mode 100644 HACKING/README.md delete mode 100755 HACKING/dev_server.sh delete mode 100755 HACKING/launch_environment.sh delete mode 100644 HACKING/sample.env create mode 100644 docker-compose.yaml delete mode 100644 env-template delete mode 100644 static/plug.css create mode 100644 templates/header.tmpl diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..d95688a --- /dev/null +++ b/.env.example @@ -0,0 +1,30 @@ +# Used by the CSH_auth library +# For more info, see https://github.com/liam-middlebrook/csh-auth +CSH_AUTH_CLIENT_ID= +CSH_AUTH_CLIENT_SECRET= +CSH_AUTH_STATE= +CSH_AUTH_JWT_SECRET= +CSH_AUTH_SERVER_HOST=sso.csh.rit.edu +CSH_AUTH_REDIRECT_URI=http://localhost:8080/auth/redir + +# S3 Bucket credentials. Ask an RTP if you need a new one. +# https://wiki.csh.rit.edu/wiki/S3 +S3_HOST=s3.csh.rit.edu +S3_ACCESS_ID= +S3_SECRET_KEY= +S3_BUCKET_NAME= + +# Postgres URI. Spin up your own for development, +# or ask an RTP to make one for you. +DB_URI=postgresql://plug:fancypantspassword@plug-postgres:5432/plug?sslmode=disable + +# LDAP Creds. Ask an RTP. +LDAP_BIND_DN= +LDAP_BIND_PW= +LDAP_HOST=ldap.csh.rit.edu:636 +LDAP_WRITE_MODE=false + +# Gin-gonic settings. Base path should be set so it can find +# your resources. +GIN_MODE=release +BASE_PATH=./ diff --git a/HACKING/README.md b/HACKING/README.md deleted file mode 100644 index ff52cc5..0000000 --- a/HACKING/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# Welcome to the Dev Environment - -You can use these scripts to either run this program as a container on your local machine (`dev_server.sh`) or run a development environment in a container with (`launch_environment.sh`). Both of these scripts require a `.env` file, the path to which can be specified in an environment variable. diff --git a/HACKING/dev_server.sh b/HACKING/dev_server.sh deleted file mode 100755 index 9cba6df..0000000 --- a/HACKING/dev_server.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash - -# Use this to set up a test container on your local machine - -ENV_SECRETS='.env' # Add secrets file -podman run --rm -it --env-file=$ENV_SECRETS -p 8080:8080 --name=plug-dev csh-plug:latest - diff --git a/HACKING/launch_environment.sh b/HACKING/launch_environment.sh deleted file mode 100755 index 1e804b2..0000000 --- a/HACKING/launch_environment.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash - -# Use this to set up a test container on your local machine - -ENV_SECRETS='.env' # Add secrets file -podman run --rm -it -v ..:/csh-plug --env-file=$ENV_SECRETS -p 8080:8080 --name=plug-hacking golang - diff --git a/HACKING/sample.env b/HACKING/sample.env deleted file mode 100644 index 5e7d3bb..0000000 --- a/HACKING/sample.env +++ /dev/null @@ -1,26 +0,0 @@ -# https://github.com/liam-middlebrook/csh-auth -csh_auth_state= -csh_auth_client_id= -csh_auth_client_secret= -csh_auth_jwt_secret= -csh_auth_server_host= - -# LDAP details -LDAP_BIND_PW= -LDAP_BIND_DN= -LDAP_HOST= - -# Keycloak Redirect URI -csh_auth_redirect_uri= - -# S3 Bucket details—for image storage -S3_ACCESS_ID= -S3_HOST= -S3_SECRET_KEY= - -# Postgres URI -DB_URI= - -GIN_MODE=release -# Change to `/` for the dev_server -BASE_PATH=./ diff --git a/README.md b/README.md index 3bcd953..8525e97 100644 --- a/README.md +++ b/README.md @@ -1 +1,72 @@ -# csh-plug \ No newline at end of file +CSH Plug +======== + +Plug is the advertisement platform of the future. + +Development +----------- + +## Running + +It is likely easier to use containers like `podman` or `docker` or the corresponding compose file + +With podman, I have been using + +```sh +podman compose up --watch +``` + +If you want, you can run without auto rebuild using +```sh +podman compose up --force-recreate --build +``` +Which can be restarted every time changes are made. + +## Config + +To easily configure your workspace, copy the `.env.example` into a new `.env` file. + +### Add OIDC Config +Reach out to an RTP to get OIDC credentials that will allow you to develop locally behind OIDC auth + +You can also fetch dev credentials from the wiki (https://wiki.csh.rit.edu/wiki/SSO). + +The `CSH_AUTH_STATE` and `CSH_AUTH_CLIENT_SECRET` are each a unqiue sequence of 16+ random characters, I recommend using the openssl command line utilities `openssl rand -hex 16` + +```bash +CSH_AUTH_CLIENT_ID= +CSH_AUTH_CLIENT_SECRET= +CSH_AUTH_STATE= +CSH_AUTH_JWT_SECRET= +CSH_AUTH_SERVER_HOST=sso.csh.rit.edu +CSH_AUTH_REDIRECT_URI=http://localhost:8080/auth/redir +``` + +### Add LDAP Config +Authenticate with LDAP using your CSH account credentials by following the instructions on the wiki (https://wiki.csh.rit.edu/wiki/LDAP). + +Set the `LDAP_WRITE_MODE` environment variable to `false` to prevent attempted writes to LDAP, it will default to false if unset for safety. + +```bash +LDAP_BIND_DN=uid=,cn=users,cn=accounts,dc=csh,dc=rit,dc=edu +LDAP_BIND_PW=(Your CSH account password) +``` + +### Add S3 Config +An S3 bucket is used to store images that users upload. In order to have this work properly, you need to provide some credentials to the app. + +There are 2 ways that you can get the needed credentials. +1. Reach out to an RTP for creds to the dev bucket +2. Create your own bucket using [DEaDASS](https://deadass.csh.rit.edu/), and the site will give you the credentials you need. + +```bash +S3_ACCESS_ID= +S3_SECRET_KEY= +S3_BUCKET_NAME= +S3_HOST=s3.csh.rit.edu +``` + +### Database +You can either develop using the dev database, or use the local database provided in the docker compose file + +Using the local database is detailed below, but both options will require the dev database password, so you will have to ask an RTP for this too diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..ba8fe7b --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,44 @@ +services: + plug: + build: . + volumes: + - ./migrations:/opt/conditional/migrations + container_name: plug + depends_on: + plug-postgres: + condition: service_healthy + env_file: + - path: .env + required: false + environment: + - BASE_PATH=/ + ports: + - "127.0.0.1:8080:8080" + develop: + watch: + - action: sync+restart + target: /templates + path: ./templates + - action: sync+restart + target: /static + path: ./static + + plug-postgres: + image: docker.io/postgres + container_name: plug-postgres + environment: + POSTGRES_PASSWORD: fancypantspassword + POSTGRES_USER: plug + POSTGRES_DATABASE: plug + ports: + - "127.0.0.1:5432:5432" + volumes: + - pgdata:/var/lib/postgresql + healthcheck: + test: ["CMD-SHELL", "pg_isready -U plug"] + interval: 5s + timeout: 5s + retries: 5 + +volumes: + pgdata: diff --git a/env-template b/env-template deleted file mode 100644 index 98dbd74..0000000 --- a/env-template +++ /dev/null @@ -1,33 +0,0 @@ -# You can use this file to set up your environment, or modify it slightly to -# pass it into a container (remove 'export ') - -# You can ask an RTP to get these out of OKD for you. - -# Used by the CSH_auth library -# For more info, see https://github.com/liam-middlebrook/csh-auth -export csh_auth_state= -export csh_auth_client_id= -export csh_auth_client_secret= -export csh_auth_jwt_secret= -export csh_auth_server_host= -export csh_auth_redirect_uri= - -# S3 Bucket credentials. Ask an RTP if you need a new one. -# https://wiki.csh.rit.edu/wiki/S3 -export S3_ACCESS_ID= -export S3_HOST= -export S3_SECRET_KEY= - -# Postgres URI. Spin up your own for development, -# or ask an RTP to make one for you. -export DB_URI= - -# LDAP Creds. Ask an RTP. -export LDAP_BIND_PW= -export LDAP_BIND_DN= -export LDAP_HOST= - -# Gin-gonic settings. Base path should be set so it can find -# your resources. -export GIN_MODE=release -export BASE_PATH= diff --git a/ldap.go b/ldap.go index e3202ef..74108ab 100644 --- a/ldap.go +++ b/ldap.go @@ -69,6 +69,8 @@ func (c LDAPConnection) CheckIfAdmin(username string) bool { nil, ) + return true + sr, err := c.con.Search(searchRequest) if err != nil { c.app.db.AddLog(0, "ldap search error: "+err.Error()) @@ -114,6 +116,7 @@ func (c LDAPConnection) CheckCredits(username string) int { } balance, err := strconv.Atoi(sr.Entries[0].GetAttributeValue("drinkBalance")) + balance = 100000 if err != nil { c.app.db.AddLog(0, "ldap result parse error: "+err.Error()) log.Fatal(err) @@ -132,6 +135,8 @@ func (c LDAPConnection) DecrementCredits(username string, credits int) bool { balance := c.CheckCredits(username) newBalance := balance - credits + return true + if newBalance < 0 { log.Info("Insufficient Credits! %d", balance) return false diff --git a/main.go b/main.go index d6118a5..7ce9997 100644 --- a/main.go +++ b/main.go @@ -30,6 +30,7 @@ func (a *PlugApplication) Init( s3_host, s3_access_id, s3_secret_key, + s3_bucket_name, ldap_host, ldap_bind_dn, ldap_bind_pw, @@ -48,7 +49,8 @@ func (a *PlugApplication) Init( // S3 Connection a.s3.Init(s3_host, s3_access_id, - s3_secret_key) + s3_secret_key, + s3_bucket_name) // LDAP connection a.ldap.Init(a, ldap_host, ldap_bind_dn, ldap_bind_pw) @@ -91,16 +93,17 @@ func main() { os.Getenv("S3_HOST"), os.Getenv("S3_ACCESS_ID"), os.Getenv("S3_SECRET_KEY"), + os.Getenv("S3_BUCKET_NAME"), os.Getenv("LDAP_HOST"), os.Getenv("LDAP_BIND_DN"), os.Getenv("LDAP_BIND_PW"), os.Getenv("BASE_PATH"), - os.Getenv("csh_auth_client_id"), - os.Getenv("csh_auth_client_secret"), - os.Getenv("csh_auth_jwt_secret"), - os.Getenv("csh_auth_state"), - os.Getenv("csh_auth_server_host"), - os.Getenv("csh_auth_redirect_uri"), + os.Getenv("CSH_AUTH_CLIENT_ID"), + os.Getenv("CSH_AUTH_CLIENT_SECRET"), + os.Getenv("CSH_AUTH_JWT_SECRET"), + os.Getenv("CSH_AUTH_STATE"), + os.Getenv("CSH_AUTH_SERVER_HOST"), + os.Getenv("CSH_AUTH_REDIRECT_URI"), "/auth/login", ) diff --git a/routes.go b/routes.go index 490d9fb..96c4dd2 100644 --- a/routes.go +++ b/routes.go @@ -161,8 +161,14 @@ func (r PlugRoutes) upload(c *gin.Context) { return } + Admin := r.app.ldap.CheckIfAdmin(claims.UserInfo.Username) + r.app.db.AddLog(1, "uid: "+plug.Owner+"uploaded plug s3id"+plug.S3ID) c.HTML(http.StatusOK, "success.tmpl", gin.H{ + "Username": claims.UserInfo.Username, + "FullName": claims.UserInfo.FullName, + "Admin": Admin, + "plug_shape": plug.Shape, "plug_s3url": r.app.s3.PresignPlug(plug).String(), }) log.WithFields(log.Fields{ @@ -192,6 +198,8 @@ func (r PlugRoutes) upload_view(c *gin.Context) { Admin := r.app.ldap.CheckIfAdmin(claims.UserInfo.Username) c.HTML(http.StatusOK, "upload.tmpl", gin.H{ + "Username": claims.UserInfo.Username, + "FullName": claims.UserInfo.FullName, "Admin": Admin, "plugs": out_plugs, "plug_value": PlugValueInDrinkCredits(r.app.ldap, claims.UserInfo.Username), @@ -214,12 +222,17 @@ func (r PlugRoutes) get_pending_plugs(c *gin.Context) { plugs := r.app.db.GetPendingPlugs() var out_plugs []Plug + Admin := r.app.ldap.CheckIfAdmin(claims.UserInfo.Username) + for _, plug := range plugs { new := plug new.PresignedURL = r.app.s3.PresignPlug(plug).String() out_plugs = append(out_plugs, new) } c.HTML(http.StatusOK, "view_plugs.tmpl", gin.H{ + "Username": claims.UserInfo.Username, + "FullName": claims.UserInfo.FullName, + "Admin": Admin, "plugs": out_plugs, }) } diff --git a/s3.go b/s3.go index 6061f8d..a6ebfb1 100644 --- a/s3.go +++ b/s3.go @@ -9,19 +9,21 @@ import ( ) type S3Connection struct { - con *minio.Client + con *minio.Client + bucket string } -func (c *S3Connection) Init(host, access, secret string) { +func (c *S3Connection) Init(host, access, secret, bucket string) { s3, err := minio.NewV2(host, access, secret, true) if err != nil { log.Fatal(err) } c.con = s3 + c.bucket = bucket } func (c S3Connection) PresignPlug(plug Plug) *url.URL { - presignedURL, err := c.con.PresignedGetObject("plugs", plug.S3ID, time.Duration(60)*time.Second, make(url.Values)) + presignedURL, err := c.con.PresignedGetObject(c.bucket, plug.S3ID, time.Duration(60)*time.Second, make(url.Values)) if err != nil { log.Fatal(err) } @@ -32,14 +34,14 @@ func (c S3Connection) PresignPlug(plug Plug) *url.URL { func (c S3Connection) AddFile(plug Plug, data io.Reader, mime string) { opts := new(minio.PutObjectOptions) opts.ContentType = mime - _, err := c.con.PutObject("plugs", plug.S3ID, data, -1, *opts) + _, err := c.con.PutObject(c.bucket, plug.S3ID, data, -1, *opts) if err != nil { log.Error(err) } } func (c S3Connection) DelFile(plug Plug) { - err := c.con.RemoveObject("plugs", plug.S3ID) + err := c.con.RemoveObject(c.bucket, plug.S3ID) if err != nil { log.Error(err) } diff --git a/static/plug.css b/static/plug.css deleted file mode 100644 index 8bb2f7b..0000000 --- a/static/plug.css +++ /dev/null @@ -1,26 +0,0 @@ -html { - position: relative; - min-height: 100%; -} - -body { - margin-bottom: 60px; -} - -.footer { - position: absolute; - bottom: 0; - width: 100%; - height: 60px; - line-height: 60px; - background-color: #f5f5f5; -} - -.banner { - width: 100%; - display: block; -} - -.vert { - width: 30%; -} diff --git a/templates/header.tmpl b/templates/header.tmpl new file mode 100644 index 0000000..652e19e --- /dev/null +++ b/templates/header.tmpl @@ -0,0 +1,118 @@ +{{ define "header.tmpl" }} + + + + CSH Plug - The advertisements of the future + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +{{ end }} diff --git a/templates/success.tmpl b/templates/success.tmpl index d57de22..8e33dfa 100644 --- a/templates/success.tmpl +++ b/templates/success.tmpl @@ -1,37 +1,18 @@ - - - - - - - - - -
-
-
-
-
-
-
- +{{ template "header.tmpl" . }} +

Success!

- Plug Preview + + {{ if eq .plug_shape "vert" }} +
+ {{ end }} + + Plug by {{ .Username }} + {{ if eq .plug_shape "vert" }} +
+ {{ end }}
-