Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cgi to go #198

Merged
merged 8 commits into from
Oct 14, 2024
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
28 changes: 14 additions & 14 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ name: CI
jobs:
build:
name: Build server image
runs-on: ubuntu-20.04
runs-on: ubuntu-latest
services:
postgres:
image: postgres:15
Expand Down Expand Up @@ -43,7 +43,7 @@ jobs:
VERSION="$VERSION-$GITHUB_REF_NAME"
fi
echo "version=$VERSION"
docker build --file ci/docker/api_Dockerfile --tag nivlheim:latest --build-arg version=$VERSION server/service
docker build --file ci/docker/api_Dockerfile --tag nivlheim:latest --build-arg version=$VERSION .
- name: Docker save
run: docker save nivlheim | gzip > nivlheim-image.tar.gz
- name: Upload artifact
Expand All @@ -54,7 +54,7 @@ jobs:

buildwww:
name: Build httpd+cgi image
runs-on: ubuntu-20.04
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
Expand All @@ -73,7 +73,7 @@ jobs:

buildclient:
name: Build client image
runs-on: ubuntu-20.04
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
Expand All @@ -93,7 +93,7 @@ jobs:
test-scripts:
name: Run test scripts
needs: [build, buildwww, buildclient]
runs-on: ubuntu-20.04
runs-on: ubuntu-latest
strategy:
matrix:
test:
Expand All @@ -115,7 +115,7 @@ jobs:
docker load --input nivlheim-image.tar.gz/nivlheim-image.tar.gz
docker load --input nivlheim-client.tar.gz/nivlheim-client.tar.gz
- name: Start containers
run: docker-compose -f "ci/docker/docker-compose.yml" up -d
run: docker compose -f "ci/docker/docker-compose.yml" up -d
- name: SSL handshake
run: openssl s_client -connect localhost:443 -prexit
continue-on-error: true
Expand All @@ -125,25 +125,25 @@ jobs:
run: tests/test_${{ matrix.test }}
- name: Retrieve logs from Docker
if: always()
run: docker-compose -f "ci/docker/docker-compose.yml" logs
run: docker compose -f "ci/docker/docker-compose.yml" logs
- name: Retrieve server logs
if: always()
run: |
echo "------- access_log -------------------------------"
docker exec docker_nivlheimweb_1 grep -v 127.0.0.1 /var/log/httpd/access_log || true
docker exec docker-nivlheimweb-1 grep -v 127.0.0.1 /var/log/httpd/access_log || true
echo "------- error_log --------------------------------"
docker exec docker_nivlheimweb_1 cat /var/log/httpd/error_log || true
docker exec docker-nivlheimweb-1 cat /var/log/httpd/error_log || true
echo "------- system.log--------------------------------"
docker exec docker_nivlheimweb_1 cat /var/log/nivlheim/system.log || true
docker exec docker-nivlheimweb-1 cat /var/log/nivlheim/system.log || true
- name: Stop containers
if: always()
run: docker-compose -f "ci/docker/docker-compose.yml" down
run: docker compose -f "ci/docker/docker-compose.yml" down

publish:
if: ${{ github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/') }}
name: Publish server image
needs: [test-scripts]
runs-on: ubuntu-20.04
runs-on: ubuntu-latest
permissions:
packages: write
contents: read
Expand Down Expand Up @@ -173,7 +173,7 @@ jobs:
if: ${{ github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/') }}
name: Publish httpd+cgi image
needs: [test-scripts]
runs-on: ubuntu-20.04
runs-on: ubuntu-latest
permissions:
packages: write
contents: read
Expand Down Expand Up @@ -212,7 +212,7 @@ jobs:
if: ${{ github.ref == 'refs/heads/master' }}
name: Tag and release
needs: [test-scripts]
runs-on: ubuntu-20.04
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.7.23
2.7.24
20 changes: 3 additions & 17 deletions ci/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,36 +1,22 @@
FROM fedora:latest
MAINTAINER iti-dia@usit.uio.no
EXPOSE 80 443
LABEL no.uio.contact=usit-gid@usit.uio.no
LABEL description="Web server with CGI scripts for Nivlheim"
LABEL description="Web server for Nivlheim"
ARG BRANCH=""

RUN dnf install -y httpd mod_ssl perl procps-ng \
RUN dnf install -y httpd mod_ssl procps-ng \
unzip file bind-utils npm \
perl-Archive-Tar perl-Archive-Zip perl-CGI perl-Crypt-OpenSSL-X509 \
perl-DBD-Pg perl-DBI perl-Digest-CRC perl-Encode \
perl-File-Basename perl-File-Path perl-File-Temp \
perl-JSON perl-Log-Log4perl perl-Log-Dispatch perl-Log-Dispatch-FileRotate \
perl-Net-CIDR perl-Net-DNS perl-Net-IP \
perl-Proc-PID-File perl-Time-Piece \
&& dnf install -y --releasever=39 openssl \
&& dnf clean all \
&& rm -rf /var/cache/yum \
&& npm install -g handlebars

# config
COPY server/openssl_ca.conf server/log4perl.conf /etc/nivlheim/
COPY server/openssl_ca.conf /etc/nivlheim/
COPY server/client_CA_cert.sh /usr/bin/
COPY server/httpd_ssl.conf /etc/httpd/conf.d/ssl.conf
COPY server/httpd.conf /etc/httpd/conf/httpd.conf

# cgi scripts
COPY server/cgi/ping server/cgi/reqcert server/cgi/processarchive /var/www/cgi-bin/
COPY server/cgi/ping2 /var/www/cgi-bin/secure/ping
COPY server/cgi/post server/cgi/renewcert /var/www/cgi-bin/secure/
COPY server/cgi/Database.pm /usr/lib64/perl5/Nivlheim/
RUN chmod -R a+x /var/www/cgi-bin/*

# copy the static web content
COPY server/website /var/www/html/
RUN rm -rf /var/www/html/mockapi
Expand Down
9 changes: 5 additions & 4 deletions ci/docker/api_Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ ARG version

WORKDIR /app

COPY go.mod go.sum ./
COPY server/service/go.mod server/service/go.sum ./
RUN go mod download

COPY *.go ./
COPY ./database ./database
COPY ./utility ./utility
COPY server/service/*.go ./
COPY server/service/database ./database
COPY server/service/utility ./utility

RUN go build -o /nivlheim -ldflags "-X main.version=${version:-UNDEFINED}"

Expand All @@ -25,5 +25,6 @@ WORKDIR /

RUN apt-get update -qq && apt-get install -yqq ca-certificates
COPY --from=build /nivlheim /nivlheim
COPY server/server.conf /etc/nivlheim/server.conf

ENTRYPOINT ["/nivlheim"]
2 changes: 1 addition & 1 deletion client/nivlheim_client
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ my $NAME = 'nivlheim_client';
my $AUTHOR = 'Øyvind Hagberg';
my $CONTACT = 'oyvind.hagberg@usit.uio.no';
my $RIGHTS = 'USIT/IT-DRIFT/GD/GID, University of Oslo, Norway';
my $VERSION = '2.7.23';
my $VERSION = '2.7.24';

# Usage text
my $USAGE = <<"END_USAGE";
Expand Down
2 changes: 1 addition & 1 deletion client/windows/nivlheim_client.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ param(
[bool]$nosleep = $false
)

Set-Variable version -option Constant -value "2.7.23"
Set-Variable version -option Constant -value "2.7.24"
Set-Variable useragent -option Constant -value "NivlheimPowershellClient/$version"
Set-PSDebug -strict
Set-StrictMode -version "Latest" # http://technet.microsoft.com/en-us/library/hh849692.aspx
Expand Down
6 changes: 6 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
nivlheim (2.7.24-1) buster; urgency=low

* Changes in the server code (cgi scripts rewritten in Go)

-- Øyvind Hagberg <oyvind.hagberg@usit.uio.no> Thu, 10 Oct 2024 09:30:00 +0200

nivlheim (2.7.23-1) buster; urgency=low

* Changes in the server code (Dependabot patches)
Expand Down
2 changes: 1 addition & 1 deletion server/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ if [ `whoami` != "root" ]; then
fi

# make dirs
mkdir -p /var/www/nivlheim/{db,certs,CA,queue}
mkdir -p /var/www/nivlheim/{db,certs,CA,queue,upload}
mkdir -p /var/log/nivlheim
mkdir -p /var/log/httpd

Expand Down
16 changes: 11 additions & 5 deletions server/httpd_ssl.conf
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,22 @@ SSLVerifyDepth 10
SSLOptions +StdEnvVars
</Files>

<Directory "/var/www/cgi-bin">
<Location "/cgi-bin/">
SSLOptions +StdEnvVars
SSLRequireSSL
</Directory>
ProxyPass "http://nivlheimapi:4040/cgi-bin/"
</Location>

<Directory "/var/www/cgi-bin/secure">
SSLOptions +StdEnvVars +ExportCertData
<Location "/cgi-bin/secure/">
SSLRequireSSL
SSLRequire %{SSL_CLIENT_VERIFY} eq "SUCCESS"
</Directory>
RequestHeader set Cert-Client-Cert "%{SSL_CLIENT_CERT}s"
RequestHeader set Cert-Client-V-Remain "%{SSL_CLIENT_V_REMAIN}s"
RequestHeader set Cert-Client-S-DN "%{SSL_CLIENT_S_DN}s"
RequestHeader set Cert-Client-I-DN "%{SSL_CLIENT_I_DN}s"
RequestHeader set Cert-Client-S-DN-CN "%{SSL_CLIENT_S_DN_CN}s"
ProxyPass "http://nivlheimapi:4040/cgi-bin/secure/"
</Location>

<Location "/api/">
ProxyPass "http://nivlheimapi:4040/api/"
Expand Down
25 changes: 0 additions & 25 deletions server/log4perl.conf

This file was deleted.

7 changes: 6 additions & 1 deletion server/server.conf
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@ LDAPprimaryAttr=
LDAPadminGroup=
AllAccessGroups=
HostOwnerPluginURL=
CFEngineKeyDir=
CFEngineKeyDir=/var/cfekeys
PGhost=
PGport=
PGdatabase=
PGuser=
PGpassword=
PGsslmode=
HTTPListenAddress=
CACertFile=CA/nivlheimca.crt
CAKeyFile=CA/nivlheimca.key
ConfDir=/var/www/nivlheim
QueueDir=/var/www/nivlheim/queue
UploadDir=/var/www/nivlheim/upload
5 changes: 5 additions & 0 deletions server/server.conf-example.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ PGdatabase=nivlheimdb
PGuser=someuser
PGpassword=abcd1234
PGsslmode=require
CACertFile=CA/nivlheimca.crt
CAKeyFile=CA/nivlheimca.key
ConfDir=/var/www/nivlheim
QueueDir=/var/www/nivlheim/queue
UploadDir=/var/www/nivlheim/upload
10 changes: 8 additions & 2 deletions server/service/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,16 @@ func createAPImuxer(theDB *sql.DB, devmode bool) *http.ServeMux {
api.Handle("/api/v2/status", &apiMethodStatus{db: theDB})
api.HandleFunc("/api/v2/userinfo", apiGetUserInfo)

// called by the nivlheim client, ported from perl
api.HandleFunc("/cgi-bin/ping", apiPing)
api.Handle("/cgi-bin/reqcert", &apiMethodReqCert{db: theDB})
api.Handle("/cgi-bin/secure/renewcert", &apiMethodRenewCert{db: theDB})
api.Handle("/cgi-bin/secure/ping", &apiMethodSecurePing{db: theDB})
api.Handle("/cgi-bin/secure/post", &apiMethodPostArchive{db: theDB})

// Add CSRF protection to all the api functions
mux.Handle("/api/v2/", wrapCSRFprotection(api))
mux.Handle("/cgi-bin/", wrapCSRFprotection(api))

// Oauth2-related endpoints
mux.HandleFunc("/api/oauth2/start", startOauth2Login)
Expand All @@ -80,8 +88,6 @@ func createAPImuxer(theDB *sql.DB, devmode bool) *http.ServeMux {
// internal API functions. Only allowed from localhost.
internal := http.NewServeMux()
internal.HandleFunc("/api/internal/triggerJob/", runJob)
internal.HandleFunc("/api/internal/unsetCurrent", unsetCurrent)
internal.HandleFunc("/api/internal/countFiles", countFiles)
internal.HandleFunc("/api/internal/replaceCertificate", replaceCertificate)
mux.Handle("/api/internal/", wrapOnlyAllowLocal(internal))

Expand Down
40 changes: 0 additions & 40 deletions server/service/api_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"net/http"
"reflect"
"regexp"
"strconv"
"strings"
)

// runJob sets the "trigger" flag on the Job struct in the jobs array,
Expand Down Expand Up @@ -37,44 +35,6 @@ func runJob(w http.ResponseWriter, req *http.Request) {
http.Error(w, "Job not found.", http.StatusNotFound)
}

// unsetCurrent is an internal API function that the CGI scripts use
// to notify the system service/daemon that some file(s) have had
// their "current" flag cleared, and can be removed from the
// in-memory search cache.
func unsetCurrent(w http.ResponseWriter, req *http.Request) {
if !isLocal(req) {
http.Error(w, "Only local requests are allowed", http.StatusForbidden)
return
}
for _, s := range strings.Split(req.FormValue("ids"), ",") {
fileID, err := strconv.ParseInt(s, 10, 64)
if err == nil {
removeFileFromFastSearch(fileID)
}
}
http.Error(w, "OK", http.StatusNoContent)
}

// countFiles is an internal API function that the CGI scripts use
// to notify the system service/daemon that a number of files
// have been processed, so we can produce an accurate count of
// files-per-minute.
func countFiles(w http.ResponseWriter, req *http.Request) {
if !isLocal(req) {
http.Error(w, "Only local requests are allowed", http.StatusForbidden)
return
}
i, err := strconv.Atoi(req.FormValue("n"))
if err != nil {
http.Error(w, "Invalid number: "+req.FormValue("n"), http.StatusBadRequest)
return
}
if i > 0 {
pfib.Add(float64(i)) // pfib = parsed files interval buffer
}
http.Error(w, "OK", http.StatusNoContent)
}

func doNothing(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
fmt.Fprintf(w, "無\n\n") // https://en.wikipedia.org/wiki/Mu_(negative)
Expand Down
3 changes: 1 addition & 2 deletions server/service/api_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ func (vars *apiMethodStatus) ServeHTTP(w http.ResponseWriter, req *http.Request)
// IncomingQueueSize
// TODO optimize for large directories
status.IncomingQueueSize = -1
const queuedir = "/var/www/nivlheim/queue"
f, err := os.Open(queuedir)
f, err := os.Open(config.QueueDir)
if err == nil {
defer f.Close()
names, err := f.Readdirnames(0)
Expand Down
Loading
Loading