Skip to content

Commit

Permalink
adds support for https in ddev router (#522)
Browse files Browse the repository at this point in the history
* adds support for https in ddev router

* bump router image for surf

* mount volume for certs

* remove base_url from settings.php, add https url to describe, fixes

* begs forgiveness of metalinter for shadow declaration and offers praises

* update web tag, only display https urls if HTTPS_EXPOSE is set

* bump for surf-windows

* update web tag and router tag

* updates cmd list test case
  • Loading branch information
tannerjfco committed Nov 17, 2017
1 parent a1bdba9 commit 97d03df
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 16 deletions.
1 change: 0 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
Changelog
=========

4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ VERSION_VARIABLES = DdevVersion WebImg WebTag DBImg DBTag RouterImage RouterTag
# These variables will be used as the default unless overridden by the make
DdevVersion ?= $(VERSION)
WebImg ?= drud/nginx-php-fpm-local
WebTag ?= v0.8.0
WebTag ?= v0.8.1
DBImg ?= drud/mysql-local-57
DBTag ?= v0.6.3
RouterImage ?= drud/ddev-router
RouterTag ?= v0.4.3
RouterTag ?= v0.5.0
DBAImg ?= drud/phpmyadmin
DBATag ?= v0.2.0

Expand Down
2 changes: 1 addition & 1 deletion cmd/ddev/cmd/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestDevList(t *testing.T) {
// Check to see that we can find our item
if item["name"] == v.Name {
found = true
assert.EqualValues(app.URL(), item["url"])
assert.Contains(item["url"], app.HostName())
assert.EqualValues(app.GetType(), item["type"])
assert.EqualValues(platform.RenderHomeRootedDir(app.AppRoot()), item["approot"])
break
Expand Down
14 changes: 13 additions & 1 deletion cmd/ddev/cmd/start.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"fmt"
"os"

"github.com/drud/ddev/pkg/dockerutil"
Expand Down Expand Up @@ -44,8 +45,19 @@ func appStart() {
util.Failed("Failed to start %s: %v", app.GetName(), err)
}

var https bool
web, err := app.FindContainerByType("web")
if err == nil {
https = dockerutil.CheckForHTTPS(web)
}

urlString := fmt.Sprintf("http://%s", app.HostName())
if https {
urlString = fmt.Sprintf("%s\nhttps://%s", urlString, app.HostName())
}

util.Success("Successfully started %s", app.GetName())
util.Success("Your application can be reached at: %s", app.URL())
util.Success("Your application can be reached at:\n%s", urlString)

}
func init() {
Expand Down
6 changes: 0 additions & 6 deletions pkg/cms/config/drupal.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,6 @@ $settings['file_scan_ignore_directories'] = [
{{ else }}
$drupal_hash_salt = '{{ $config.HashSalt }}';
$base_url = '{{ $config.DeployURL }}';
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &&
$_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
$_SERVER['HTTPS'] = 'on';
}
{{ end }}
Expand Down
3 changes: 3 additions & 0 deletions pkg/ddevapp/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ services:
# HTTP_EXPOSE allows for ports accepting HTTP traffic to be accessible from <site>.ddev.local:<port>
# To expose a container port to a different host port, define the port as hostPort:containerPort
- HTTP_EXPOSE=80,{{ .mailhogport }}
# You can optionally expose an HTTPS port option for any ports defined in HTTP_EXPOSE.
# To expose an HTTPS port, define the port as securePort:containerPort.
- HTTPS_EXPOSE=443:80
labels:
com.ddev.site-name: ${DDEV_SITENAME}
com.ddev.platform: {{ .plugin }}
Expand Down
10 changes: 10 additions & 0 deletions pkg/dockerutil/dockerutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,13 @@ func GetPublishedPort(privatePort int64, container docker.APIContainers) int64 {
}
return 0
}

// CheckForHTTPS determines if a container has the HTTPS_EXPOSE var
// set to route 443 traffic to 80
func CheckForHTTPS(container docker.APIContainers) bool {
env := GetContainerEnv("HTTPS_EXPOSE", container)
if env != "" && strings.Contains(env, "443:80") {
return true
}
return false
}
12 changes: 11 additions & 1 deletion pkg/plugins/platform/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,21 @@ func (l *LocalApp) Describe() (map[string]interface{}, error) {
shortRoot := RenderHomeRootedDir(l.AppRoot())
appDesc := make(map[string]interface{})

var https bool
urlString := fmt.Sprintf("http://%s", l.HostName())
webCon, err := l.FindContainerByType("web")
if err == nil {
https = dockerutil.CheckForHTTPS(webCon)
}
if https {
urlString = fmt.Sprintf("%s\nhttps://%s", urlString, l.HostName())
}

appDesc["name"] = l.GetName()
appDesc["status"] = l.SiteStatus()
appDesc["type"] = l.GetType()
appDesc["approot"] = shortRoot
appDesc["url"] = l.URL()
appDesc["url"] = urlString

db, err := l.FindContainerByType("db")
if err != nil {
Expand Down
16 changes: 15 additions & 1 deletion pkg/plugins/platform/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,15 @@ func StartDdevRouter() error {
routerdir := filepath.Dir(dest)
err := os.MkdirAll(routerdir, 0755)
if err != nil {
return fmt.Errorf("unable to create directory for ddev router: %s", err)
return fmt.Errorf("unable to create directory for ddev router: %v", err)
}

certDir := filepath.Join(util.GetGlobalDdevDir(), "certs")
if _, err = os.Stat(certDir); os.IsNotExist(err) {
err = os.MkdirAll(certDir, 0755)
if err != nil {
return fmt.Errorf("unable to create directory for ddev router: %v", err)
}
}

var doc bytes.Buffer
Expand Down Expand Up @@ -173,6 +181,12 @@ func determineRouterPorts() []string {
exposePorts = append(exposePorts, ports...)
}

httpsPorts := dockerutil.GetContainerEnv("HTTPS_EXPOSE", container)
if httpsPorts != "" {
ports := strings.Split(httpsPorts, ",")
exposePorts = append(exposePorts, ports...)
}

for _, exposePort := range exposePorts {
// ports defined as hostPort:containerPort allow for router to configure upstreams
// for containerPort, with server listening on hostPort. exposed ports for router
Expand Down
1 change: 1 addition & 0 deletions pkg/plugins/platform/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ services:
{{ end }}
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
- ./certs:/etc/nginx/certs:cached
restart: always
networks:
default:
Expand Down
3 changes: 2 additions & 1 deletion pkg/plugins/platform/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ func CreateAppTable() *uitable.Table {
table := uitable.New()
table.MaxColWidth = 140
table.Separator = " "
table.AddRow("NAME", "TYPE", "LOCATION", "URL", "STATUS")
table.Wrap = true
table.AddRow("NAME", "TYPE", "LOCATION", "URL(s)", "STATUS")
return table
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ var DdevVersion = "v0.3.0-dev" // Note that this is overridden by make
var DockerVersionConstraint = ">= 17.05.0-ce"

// WebImg defines the default web image used for applications.
var WebImg = "drud/nginx-php-fpm7-local" // Note that this is overridden by make
var WebImg = "drud/nginx-php-fpm-local" // Note that this is overridden by make

// WebTag defines the default web image tag for drud dev
var WebTag = "v0.7.4" // Note that this is overridden by make
var WebTag = "v0.8.0" // Note that this is overridden by make

// DBImg defines the default db image used for applications.
var DBImg = "drud/mysql-local-57" // Note that this is overridden by make
Expand Down

0 comments on commit 97d03df

Please sign in to comment.