Skip to content

Commit

Permalink
Merge pull request #243 from ugexe/nlogan/env-vars
Browse files Browse the repository at this point in the history
geoipupdate supports environment variables for config options
  • Loading branch information
horgh authored Jul 10, 2023
2 parents 12d3f04 + 007a68c commit 972a166
Show file tree
Hide file tree
Showing 9 changed files with 736 additions and 136 deletions.
28 changes: 28 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
# CHANGELOG

## 6.0.0

* `geoipupdate` now supports configuration via environment variables. Any
configuration set this way will override any value from the config file,
but still be overridden by any associated command line option (if any).
The following new environment variables are supported:

* `GEOIPUPDATE_ACCOUNT_ID`
* `GEOIPUPDATE_ACCOUNT_ID_FILE`
* `GEOIPUPDATE_CONF_FILE`
* `GEOIPUPDATE_DB_DIR`
* `GEOIPUPDATE_EDITION_IDS`
* `GEOIPUPDATE_HOST`
* `GEOIPUPDATE_LICENSE_KEY`
* `GEOIPUPDATE_LICENSE_KEY_FILE`
* `GEOIPUPDATE_LOCK_FILE`
* `GEOIPUPDATE_PARALLELISM`
* `GEOIPUPDATE_PRESERVE_FILE_TIMES`
* `GEOIPUPDATE_PROXY`
* `GEOIPUPDATE_PROXY_USER_PASSWORD`
* `GEOIPUPDATE_RETRY_FOR`
* `GEOIPUPDATE_VERBOSE`

* Changed the signature of `NewConfig` in `pkg/geoipupdate` to no longer accept
a positional config file path argument, which can now be passed in using the
option from `WithConfigFile` along with the other optional parameters.
* `geoipupdate` and `NewConfig` no longer require a config file to exist.

## 5.1.1 (2023-05-08)

* Based on feedback, the change to use a non-root user in 5.1.0
Expand Down
12 changes: 6 additions & 6 deletions cmd/geoipupdate/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@ type Args struct {
}

func getArgs() *Args {
confFileDefault := vars.DefaultConfigFile
if value, ok := os.LookupEnv("GEOIPUPDATE_CONF_FILE"); ok {
confFileDefault = value
}

configFile := flag.StringP(
"config-file",
"f",
vars.DefaultConfigFile,
confFileDefault,
"Configuration file",
)
databaseDirectory := flag.StringP(
Expand All @@ -49,11 +54,6 @@ func getArgs() *Args {
os.Exit(0)
}

if *configFile == "" {
log.Printf("You must provide a configuration file.")
printUsage()
}

if *parallelism < 0 {
log.Printf("Parallelism must be a positive number")
printUsage()
Expand Down
5 changes: 2 additions & 3 deletions cmd/geoipupdate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main

import (
"context"
"fmt"
"log"
"os"

Expand Down Expand Up @@ -41,14 +40,14 @@ func main() {
}

config, err := geoipupdate.NewConfig(
args.ConfigFile,
geoipupdate.WithConfigFile(args.ConfigFile),
geoipupdate.WithDatabaseDirectory(args.DatabaseDirectory),
geoipupdate.WithParallelism(args.Parallelism),
geoipupdate.WithVerbose(args.Verbose),
geoipupdate.WithOutput(args.Output),
)
if err != nil {
fatalLogger(fmt.Sprintf("error loading configuration file %s", args.ConfigFile), err)
fatalLogger("error loading configuration", err)
}

if config.Verbose {
Expand Down
46 changes: 30 additions & 16 deletions doc/GeoIP.conf.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,66 +17,80 @@ sensitive.

`AccountID`

: Your MaxMind account ID. This was formerly known as `UserId`.
: Your MaxMind account ID. This was formerly known as `UserId`. This can be
overridden at run time by either the `GEOIPUPDATE_ACCOUNT_ID` or the
`GEOIPUPDATE_ACCOUNT_ID_FILE` environment variables.

`LicenseKey`

: Your case-sensitive MaxMind license key.
: Your case-sensitive MaxMind license key. This can be overridden at run time
by either the `GEOIPUPDATE_LICENSE_KEY` or `GEOIPUPDATE_LICENSE_KEY_FILE`
environment variables.

`EditionIDs`

: List of space-separated database edition IDs. Edition IDs may consist
of letters, digits, and dashes. For example, `GeoIP2-City` would
download the GeoIP2 City database (`GeoIP2-City`). Note: this was
formerly called `ProductIds`.
download the GeoIP2 City database (`GeoIP2-City`). This can be overridden
at run time by the `GEOIPUPDATE_EDITION_IDS` environment variable. Note:
this was formerly called `ProductIds`.

## Optional settings:

`DatabaseDirectory`

: The directory to store the database files. If not set, the default is
DATADIR. This can be overridden at run time by the `-d` command line
argument.
DATADIR. This can be overridden at run time by the `GEOIPUPDATE_DB_DIR`
environment variable or the `-d` command line argument.

`Host`

: The host name of the server to use. The default is `updates.maxmind.com`.
This can be overridden at run time by the `GEOIPUPDATE_HOST` environment
variable.

`Proxy`

: The proxy host name or IP address. You may optionally specify
a port number, e.g., `127.0.0.1:8888`. If no port number is specified,
1080 will be used.
: The proxy host name or IP address. You may optionally specify a port
number, e.g., `127.0.0.1:8888`. If no port number is specified, 1080
will be used. This can be overridden at run time by the
`GEOIPUPDATE_PROXY` environment variable.

`ProxyUserPassword`

: The proxy user name and password, separated by a colon. For instance,
`username:password`.
`username:password`. This can be overridden at run time by the
`GEOIPUPDATE_PROXY_USER_PASSWORD` environment variable.

`PreserveFileTimes`

: Whether to preserve modification times of files downloaded from the
server. This option is either `0` or `1`. The default is `0`.
server. This option is either `0` or `1`. The default is `0`. This
can be overridden at run time by the `GEOIPUPDATE_PRESERVE_FILE_TIMES`
environment variable.

`LockFile`

: The lock file to use. This ensures only one `geoipupdate` process can run
at a time. Note: Once created, this lockfile is not removed from the
filesystem. The default is `.geoipupdate.lock` under the
`DatabaseDirectory`.
`DatabaseDirectory`. This can be overridden at run time by the
`GEOIPUPDATE_LOCK_FILE` environment variable.

`RetryFor`

: The amount of time to retry for when errors during HTTP transactions are
encountered. It can be specified as a (possibly fractional) decimal number
followed by a unit suffix. Valid time units are `ns`, `us` (or `µs`), `ms`,
`s`, `m`, `h`. The default is `5m` (5 minutes).
`s`, `m`, `h`. The default is `5m` (5 minutes). This can be overridden at
run time by the `GEOIPUPDATE_RETRY_FOR` environment variable.

`Parallelism`

: The maximum number of parallel database downloads. The default is
1, which means that databases will be downloaded sequentially. This can be
overriden at runtime by the `--parallelism` command line argument.
: The maximum number of parallel database downloads. The default is
1, which means that databases will be downloaded sequentially. This can be
overridden at run time by the `GEOIPUPDATE_PARALLELISM` environment
variable or the `--parallelism` command line argument.

## Deprecated settings:

Expand Down
19 changes: 13 additions & 6 deletions doc/docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,23 @@ The source code is available on [GitHub](https://github.com/maxmind/geoipupdate)
The Docker image is configured by environment variables. The following
variables are required:

* `GEOIPUPDATE_ACCOUNT_ID` - Your MaxMind account ID.
* `GEOIPUPDATE_LICENSE_KEY` - Your case-sensitive MaxMind license key.
* `GEOIPUPDATE_EDITION_IDS` - List of space-separated database edition IDs.
Edition IDs may consist of letters, digits, and dashes. For example,
`GeoIP2-City` would download the GeoIP2 City database (`GeoIP2-City`).

One of:

* `GEOIPUPDATE_ACCOUNT_ID` - Your MaxMind account ID.
* `GEOIPUPDATE_ACCOUNT_ID_FILE` - A file containing your MaxMind account ID.

One of:

* `GEOIPUPDATE_LICENSE_KEY` - Your case-sensitive MaxMind license key.
* `GEOIPUPDATE_LICENSE_KEY_FILE` - A file containing your case-sensitive
MaxMind license key.

The following are optional:

* `GEOIPUPDATE_ACCOUNT_ID_FILE` - The path to a file containing your MaxMind account ID. This is intended to be used with Docker secrets (example below).
* `GEOIPUPDATE_LICENSE_KEY_FILE` - The path to a file containing your case-sensitive MaxMind license key. This is intended to be used with Docker secrets (example below).
* `GEOIPUPDATE_FREQUENCY` - The number of hours between `geoipupdate` runs.
If this is not set or is set to `0`, `geoipupdate` will run once and exit.
* `GEOIPUPDATE_HOST` - The host name of the server to use. The default is
Expand All @@ -34,8 +41,8 @@ The following are optional:
default is `0`.
* `GEOIPUPDATE_VERBOSE` - Enable verbose mode. Prints out the steps that
`geoipupdate` takes. Set to **anything** (e.g., `1`) to enable.
* `GEOIPUPDATE_CONF_FILE` - The path where the configuration file will be
written. The default is `/etc/GeoIP.conf`.
* `GEOIPUPDATE_CONF_FILE` - The path of a configuration file to be used by
`geoipupdate`.
* `GEOIPUPDATE_DB_DIR` - The directory where geoipupdate will download the
databases. The default is `/usr/share/GeoIP`.

Expand Down
15 changes: 9 additions & 6 deletions doc/geoipupdate.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ open.
`-d`, `--database-directory`

: Install databases to a custom directory. This is optional. If provided, it
overrides any `DatabaseDirectory` set in the configuration file.
overrides the `DatabaseDirectory` value from the configuration file and the
`GEOIPUPDATE_DB_DIR` environment variable.

`-f`, `--config-file`

: The configuration file to use. See `GeoIP.conf` and its documentation for
more information. This is optional. It defaults to CONFFILE.
more information. This is optional. It defaults to the environment variable
`GEOIPUPDATE_CONF_FILE` if it is set, or CONFFILE otherwise.

`--parallelism`

Expand All @@ -47,7 +49,8 @@ open.

`-v`, `--verbose`

: Enable verbose mode. Prints out the steps that `geoipupdate` takes.
: Enable verbose mode. Prints out the steps that `geoipupdate` takes. If
provided, it overrides any `GEOIPUPDATE_VERBOSE` environment variable.

`-o`, `--output`

Expand All @@ -72,9 +75,9 @@ runs `geoipupdate` on each Wednesday at noon:
# end of crontab


To use with a proxy server, update your `GeoIP.conf` file as specified
in the `GeoIP.conf` man page or set the `http_proxy` environment
variable.
To use with a proxy server, update your `GeoIP.conf` file as specified in
the `GeoIP.conf` man page. Alternatively, set the `GEOIPUPDATE_PROXY` or
`http_proxy` environment variable.

# BUGS

Expand Down
53 changes: 11 additions & 42 deletions docker/entry.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,67 +14,36 @@ term_handler() {
trap 'kill ${!}; term_handler' SIGTERM

pid=0
conf_file=/var/lib/geoipupdate/GeoIP.conf
database_dir=/usr/share/GeoIP
log_dir="/var/lib/geoipupdate"
log_file="$log_dir/.healthcheck"
flags="--output"
frequency=$((GEOIPUPDATE_FREQUENCY * 60 * 60))

if ! [ -z "$GEOIPUPDATE_CONF_FILE" ]; then
conf_file=$GEOIPUPDATE_CONF_FILE
if [ -z "$GEOIPUPDATE_DB_DIR" ]; then
GEOIPUPDATE_DB_DIR="$database_dir"
fi

if ! [ -z "$GEOIPUPDATE_DB_DIR" ]; then
database_dir=$GEOIPUPDATE_DB_DIR
fi

if [ ! -z "$GEOIPUPDATE_ACCOUNT_ID_FILE" ]; then
GEOIPUPDATE_ACCOUNT_ID=$( cat "$GEOIPUPDATE_ACCOUNT_ID_FILE" )
fi

if [ ! -z "$GEOIPUPDATE_LICENSE_KEY_FILE" ]; then
GEOIPUPDATE_LICENSE_KEY=$( cat "$GEOIPUPDATE_LICENSE_KEY_FILE" )
fi

if [ -z "$GEOIPUPDATE_ACCOUNT_ID" ] || [ -z "$GEOIPUPDATE_LICENSE_KEY" ] || [ -z "$GEOIPUPDATE_EDITION_IDS" ]; then
echo "ERROR: You must set the environment variables GEOIPUPDATE_ACCOUNT_ID, GEOIPUPDATE_LICENSE_KEY, and GEOIPUPDATE_EDITION_IDS!"
if [ -z "$GEOIPUPDATE_ACCOUNT_ID" ] && [ -z "$GEOIPUPDATE_ACCOUNT_ID_FILE" ]; then
echo "ERROR: You must set the environment variable GEOIPUPDATE_ACCOUNT_ID or GEOIPUPDATE_ACCOUNT_ID_FILE!"
exit 1
fi

# Create configuration file
echo "# STATE: Creating configuration file at $conf_file"
cat <<EOF > "$conf_file"
AccountID $GEOIPUPDATE_ACCOUNT_ID
LicenseKey $GEOIPUPDATE_LICENSE_KEY
EditionIDs $GEOIPUPDATE_EDITION_IDS
EOF

if [ ! -z "$GEOIPUPDATE_HOST" ]; then
echo "Host $GEOIPUPDATE_HOST" >> "$conf_file"
fi

if [ ! -z "$GEOIPUPDATE_PROXY" ]; then
echo "Proxy $GEOIPUPDATE_PROXY" >> "$conf_file"
fi

if [ ! -z "$GEOIPUPDATE_PROXY_USER_PASSWORD" ]; then
echo "ProxyUserPassword $GEOIPUPDATE_PROXY_USER_PASSWORD" >> "$conf_file"
fi

if [ ! -z "$GEOIPUPDATE_PRESERVE_FILE_TIMES" ]; then
echo "PreserveFileTimes $GEOIPUPDATE_PRESERVE_FILE_TIMES" >> "$conf_file"
if [ -z "$GEOIPUPDATE_LICENSE_KEY" ] && [ -z "$GEOIPUPDATE_LICENSE_KEY_FILE" ]; then
echo "ERROR: You must set the environment variable GEOIPUPDATE_LICENSE_KEY or GEOIPUPDATE_LICENSE_KEY_FILE!"
exit 1
fi

if [ "$GEOIPUPDATE_VERBOSE" ]; then
flags="$flags -v"
if [ -z "$GEOIPUPDATE_EDITION_IDS" ]; then
echo "ERROR: You must set the environment variable GEOIPUPDATE_EDITION_IDS!"
exit 1
fi

mkdir -p $log_dir

while true; do
echo "# STATE: Running geoipupdate"
/usr/bin/geoipupdate -d "$database_dir" -f "$conf_file" $flags 1>$log_file
/usr/bin/geoipupdate $flags 1>$log_file
if [ "$frequency" -eq 0 ]; then
break
fi
Expand Down
Loading

0 comments on commit 972a166

Please sign in to comment.