Skip to content

Commit

Permalink
Listen with TLS if configured (#746)
Browse files Browse the repository at this point in the history
Add flags to enable listening on TLS.
  • Loading branch information
jsoriano authored Oct 14, 2021
1 parent c214102 commit 32109b3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* Configuration file path can be selected with the `-config` flag. [#745](https://github.com/elastic/package-registry/pull/745)
* Configuration flags can be provided using environment variables. [#745](https://github.com/elastic/package-registry/pull/745)
* Add `-tls-cert` and `-tls-key` flags to configure HTTPS. [#711](https://github.com/elastic/package-registry/issues/711) [#746](https://github.com/elastic/package-registry/issues/746)

### Deprecated

Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ EXPOSE 8080
ENTRYPOINT ["./package-registry"]

# Make sure it's accessible from outside the container
CMD ["--address=0.0.0.0:8080"]
ENV EPR_ADDRESS=0.0.0.0:8080

HEALTHCHECK --interval=1s --retries=30 CMD curl --silent --fail localhost:8080/health || exit 1

13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,18 @@ docker run -p 8080:8080 {image id from prior step}
**Commands ready to cut-and-paste**
```
docker build --rm -t docker.elastic.co/package-registry/package-registry:master .
docker run -i -t -p 8080:8080 $(docker images -q docker.elastic.co/package-registry/package-registry:master)
docker run -it -p 8080:8080 $(docker images -q docker.elastic.co/package-registry/package-registry:master)
```

**Listening on HTTPS**
```
docker run -it -p 8443:8443 \
-v /etc/ssl/package-registry.key:/etc/ssl/package-registry.key:ro \
-v /etc/ssl/package-registry.crt:/etc/ssl/package-registry.crt:ro \
-e EPR_ADDRESS=0.0.0.0:8443
-e EPR_TLS_KEY=/etc/ssl/package-registry.key \
-e EPR_TLS_CERT=/etc/ssl/package-registry.crt \
docker.elastic.co/package-registry/package-registry:master
```

#### Docker images published
Expand Down
14 changes: 13 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ var (
address string
httpProfAddress string

tlsCertFile string
tlsKeyFile string

dryRun bool
configPath string

Expand All @@ -50,6 +53,8 @@ var (

func init() {
flag.StringVar(&address, "address", "localhost:8080", "Address of the package-registry service.")
flag.StringVar(&tlsCertFile, "tls-cert", "", "Path of the TLS certificate.")
flag.StringVar(&tlsKeyFile, "tls-key", "", "Path of the TLS key.")
flag.StringVar(&configPath, "config", "config.yml", "Path to the configuration file.")
flag.StringVar(&httpProfAddress, "httpprof", "", "Enable HTTP profiler listening on the given address.")
// This flag is experimental and might be removed in the future or renamed
Expand All @@ -74,7 +79,7 @@ func main() {

server := initServer()
go func() {
err := server.ListenAndServe()
err := runServer(server)
if err != nil && err != http.ErrServerClosed {
log.Fatalf("Error occurred while serving: %s", err)
}
Expand Down Expand Up @@ -130,6 +135,13 @@ func initServer() *http.Server {
return &http.Server{Addr: address, Handler: router}
}

func runServer(server *http.Server) error {
if tlsCertFile != "" && tlsKeyFile != "" {
return server.ListenAndServeTLS(tlsCertFile, tlsKeyFile)
}
return server.ListenAndServe()
}

func initAPMTracer() *apm.Tracer {
apm.DefaultTracer.Close()
if _, found := os.LookupEnv("ELASTIC_APM_SERVER_URL"); !found {
Expand Down

0 comments on commit 32109b3

Please sign in to comment.