From 3dcf9b178a68d7c4019119fc951826785c1ef0fa Mon Sep 17 00:00:00 2001 From: Jacky Date: Tue, 4 Jul 2023 00:23:09 +0800 Subject: [PATCH 1/2] Add heroku.yml Init the db set the port set the port in the src Remove entry point Remove init: run heroku remove run from heroku.yml copy run-heroku change to 3000 databunker-heroku edit --- Dockerfile | 6 ++-- databunker-heroku.yaml | 80 ++++++++++++++++++++++++++++++++++++++++++ heroku.yml | 5 +++ run-heroku.sh | 16 +++++++++ src/bunker.go | 2 +- 5 files changed, 105 insertions(+), 4 deletions(-) create mode 100644 databunker-heroku.yaml create mode 100644 heroku.yml create mode 100755 run-heroku.sh diff --git a/Dockerfile b/Dockerfile index e908e24c9..4b67cd488 100644 --- a/Dockerfile +++ b/Dockerfile @@ -23,7 +23,7 @@ COPY --from=builder /lib/ld* /lib/libssl.* /lib/libcrypto.* /lib/ COPY --from=builder /etc/group /etc/ COPY --from=builder /etc/ssl /etc/ssl -COPY databunker.yaml /databunker/conf/ +COPY databunker*.yaml /databunker/conf/ RUN /bin/busybox mkdir -p /databunker/data && \ /bin/busybox mkdir -p /databunker/certs && \ /bin/busybox ln -s /bin/busybox /bin/addgroup && \ @@ -35,8 +35,8 @@ RUN /bin/busybox mkdir -p /databunker/data && \ addgroup -S appgroup && adduser --no-create-home -S appuser -G appgroup && \ chown appuser:appgroup /databunker/data USER appuser -COPY --from=builder /go/bin/databunker /go/src/securitybunker/databunker/run.sh /go/src/securitybunker/databunker/health-check.sh /databunker/bin/ +COPY --from=builder /go/bin/databunker /go/src/securitybunker/databunker/run*.sh /go/src/securitybunker/databunker/health-check.sh /databunker/bin/ EXPOSE 3000 HEALTHCHECK --interval=5s --timeout=3s --start-period=33s --retries=3 CMD /databunker/bin/health-check.sh -ENTRYPOINT ["/bin/sh", "/databunker/bin/run.sh"] +ENTRYPOINT ["/bin/sh", "/databunker/bin/run-heroku.sh"] #CMD ["/bin/sh", "-x", "-c", "/go/bin/databunker -init"] diff --git a/databunker-heroku.yaml b/databunker-heroku.yaml new file mode 100644 index 000000000..b9cc672e4 --- /dev/null +++ b/databunker-heroku.yaml @@ -0,0 +1,80 @@ +# Server configurations +generic: + # allow to create user object without login + create_user_without_access_token: true + # use separate app tables, default false + # use_separate_app_tables: true + # specify if API call to list users is available (default false) + # list_users: true +selfservice: + # specifies if user can remove himself withour Admin/DPO approval (default false) + forget_me: false + # specifies if user can change his data without Admin/DPO approval (default false) + user_record_change: true + # specifies a list of app-data objects user can change without approval + app_record_change: ["*"] +notification: + # url that receives notifications: + # - bad login + # - forget me + # - user profile change + # - consent change + # user details are send as json data. + #notification_url: "https://httpbin.org/post" +policy: + # max time to store records, untill they are deleted + max_user_retention_period: "3m" + max_audit_retention_period: "6m" + max_session_retention_period: "1h" + max_shareable_record_retention_period: "1m" +#ssl: +# # ssl configuration +# ssl_certificate: "/databunker/certs/server.cer" +# ssl_certificate_key: "/databunker/certs/server.key" +sms: + # REQUIRED: Specify SMS gateway URL. + # Available modifiers for url parameters: {token}, {from}, {phone}, {msg} + # url: "https://sms-gateway.com/send?token=_TOKEN_&from=_FROM_&phone=_PHONE_&msg=_MSG_" + # OPTIONAL: Specify From address + # from: "" + # OPTIONAL: Specify SMS gateway access token + # token: "" + # OPTIONAL: Specify SMS gateway Basic Auth header, format: username:password + # basic_auth: "user:pssword" + # OPTIONAL: Specify SMS gateway HTTP request method: GET or POST. Default is GET + # method: "GET" + # OPTIONAL: Specify SMS gateway HTTP request custom header. Can be used for authorization. + # custom_header: "x-token: value" + # OTIONAL: Specify SMS gateway HTTP request Content Type. It can be application/json. + # By default application/x-www-form-urlencoded + # content_type: "json" + # REQUIRED FOR POST REQUEST: POST body. Available modifiers: _TOKEN_, _FROM_, _PHONE_, _MSG_ + # Example for application/json POST request: + # body: '{"from":"_FROM_","phone":"_PHONE_","message":"_MSG_"}' + # Example for application/x-www-form-urlencoded POST request + # body: 'from=_FROM_&phone=_PHONE_&message=_MSG_' + # default country when sending out SMS + # Use country code when normalizing phone numbers. Use country code as defined here: + # https://github.com/ttacon/libphonenumber/blob/master/countrycodetoregionmap.go + default_country: "GB" +server: + host: "0.0.0.0" +# port: 3000 +smtp: + # You need to get SMTP server to send out email notification for example to allow user login. + # You can look for a email service company offering SMTP services. You can pick from here: + # https://privacybunker.io/blog/european-cloud-saas-vendors/ + server: "email-smtp-server.com" + port: 587 + user: "user@your-company.com" + pass: "" + sender: "bot@databunker.your-company.com" +ui: + logo_link: "/site/img/logo-example.png" + company_title: "Your company here" + company_link: "https://privacybunker.io/" + term_of_service_title: "Your Terms of service link" + term_of_service_link: "https://privacybunker.io/" + privacy_policy_title: "Your Privacy Policy link" + privacy_policy_link: "https://privacybunker.io/" + custom_css_link: "" diff --git a/heroku.yml b/heroku.yml new file mode 100644 index 000000000..7a55bdc22 --- /dev/null +++ b/heroku.yml @@ -0,0 +1,5 @@ +build: + docker: + web: Dockerfile + + diff --git a/run-heroku.sh b/run-heroku.sh new file mode 100755 index 000000000..97dfbdbc6 --- /dev/null +++ b/run-heroku.sh @@ -0,0 +1,16 @@ +#!/bin/sh + +if [ -z "$DATABUNKER_MASTERKEY" ]; then + echo "DATABUNKER_MASTERKEY environment value is empty" + /bin/busybox sleep 60 + exit +fi + +echo "-------------ENV-------------" +/bin/busybox env + +echo "-------------FIND------------" +/bin/busybox find /databunker + +echo "-------------RUN-------------" +/databunker/bin/databunker -start -db $PGSQL_DB -conf /databunker/conf/databunker-heroku.yaml diff --git a/src/bunker.go b/src/bunker.go index 952fd428d..6ae0b0f7d 100644 --- a/src/bunker.go +++ b/src/bunker.go @@ -80,7 +80,7 @@ type Config struct { DefaultCountry string `yaml:"default_country"` } Server struct { - Port string `yaml:"port" envconfig:"BUNKER_PORT"` + Port string `yaml:"port" envconfig:"PORT"` Host string `yaml:"host" envconfig:"BUNKER_HOST"` } `yaml:"server"` SMTP struct { From 8638c6186a3d794b0aa5b3b9aaedcb33ef34ecba Mon Sep 17 00:00:00 2001 From: Jacky Date: Wed, 20 Nov 2024 02:00:23 +0800 Subject: [PATCH 2/2] detect DATABASE_URL that consist of postgres --- Dockerfile | 2 +- src/storage/pgsql-storage.go | 17 +++++++++++++++++ src/storage/storage.go | 13 ++++++++++++- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 4b67cd488..fb2196e38 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ ############################ # STEP 1 build executable binary ############################ -FROM golang:alpine AS builder +FROM golang:1.19.10-alpine3.18 AS builder RUN apk update && apk add --no-cache git gcc libc-dev openssl && go install github.com/gobuffalo/packr/packr@latest WORKDIR $GOPATH/src/securitybunker/databunker/src/ COPY src/go.mod ./deps diff --git a/src/storage/pgsql-storage.go b/src/storage/pgsql-storage.go index b5d68693a..4659745ca 100644 --- a/src/storage/pgsql-storage.go +++ b/src/storage/pgsql-storage.go @@ -8,6 +8,7 @@ import ( "io/ioutil" "log" "net/http" + "net/url" "os" "strconv" "strings" @@ -24,6 +25,22 @@ type PGSQLDB struct { } func (dbobj PGSQLDB) getConnectionString(dbname *string) string { + databaseURL := os.Getenv("DATABASE_URL") + if len(databaseURL) > 0 { + u, err := url.Parse(databaseURL) + if err == nil && u.Scheme == "postgres" { + // Extract user info, host, port, and dbname from the URL + user := u.User.Username() + pass, _ := u.User.Password() + host := u.Hostname() + port := u.Port() + dbname := strings.TrimPrefix(u.Path, "/") + + return fmt.Sprintf("user='%s' password='%s' host='%s' port='%s' dbname='%s'", + user, pass, host, port, dbname) + } + } + user := os.Getenv("PGSQL_USER_NAME") pass := os.Getenv("PGSQL_USER_PASS") host := os.Getenv("PGSQL_HOST") diff --git a/src/storage/storage.go b/src/storage/storage.go index 3de9f176f..a9377bfa0 100644 --- a/src/storage/storage.go +++ b/src/storage/storage.go @@ -3,6 +3,7 @@ package storage import ( "go.mongodb.org/mongo-driver/bson" "net/http" + "net/url" "os" ) @@ -104,8 +105,18 @@ type BackendDB interface { } func getDBObj() BackendDB { - host := os.Getenv("MYSQL_HOST") var db BackendDB + databaseURL := os.Getenv("DATABASE_URL") + // Check if DATABASE_URL is set and is a PostgreSQL URL + if len(databaseURL) > 0 { + u, err := url.Parse(databaseURL) + if err == nil && u.Scheme == "postgres" { + db = &PGSQLDB{} + return db + } + } + + host := os.Getenv("MYSQL_HOST") if len(host) > 0 { db = &MySQLDB{} return db