mirror of https://github.com/portainer/portainer
Persist csrf authKey in container to allow restarts without breaking existing cookies.
parent
0244bc7317
commit
5f3d856535
29
dockerui.go
29
dockerui.go
|
@ -11,17 +11,17 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"github.com/gorilla/csrf"
|
"github.com/gorilla/csrf"
|
||||||
|
"io/ioutil"
|
||||||
|
"fmt"
|
||||||
|
"github.com/gorilla/securecookie"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
endpoint = flag.String("e", "/var/run/docker.sock", "Dockerd endpoint")
|
endpoint = flag.String("e", "/var/run/docker.sock", "Dockerd endpoint")
|
||||||
addr = flag.String("p", ":9000", "Address and port to serve dockerui")
|
addr = flag.String("p", ":9000", "Address and port to serve dockerui")
|
||||||
assets = flag.String("a", ".", "Path to the assets")
|
assets = flag.String("a", ".", "Path to the assets")
|
||||||
CSRF = csrf.Protect(
|
authKey []byte
|
||||||
[]byte("32-byte-long-auth-key"), // FIXME: generate once, reuse on restarts
|
authKeyFile = "authKey.dat"
|
||||||
csrf.HttpOnly(false),
|
|
||||||
csrf.Secure(false),
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type UnixHandler struct {
|
type UnixHandler struct {
|
||||||
|
@ -91,6 +91,25 @@ func createHandler(dir string, e string) http.Handler {
|
||||||
h = createUnixHandler(e)
|
h = createUnixHandler(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Use existing csrf authKey if present or generate a new one.
|
||||||
|
dat, err := ioutil.ReadFile(authKeyFile)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
authKey = securecookie.GenerateRandomKey(32)
|
||||||
|
err := ioutil.WriteFile(authKeyFile, authKey, 0644)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("unable to persist auth key", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
authKey = dat
|
||||||
|
}
|
||||||
|
|
||||||
|
CSRF := csrf.Protect(
|
||||||
|
authKey,
|
||||||
|
csrf.HttpOnly(false),
|
||||||
|
csrf.Secure(false),
|
||||||
|
)
|
||||||
|
|
||||||
mux.Handle("/dockerapi/", http.StripPrefix("/dockerapi", h))
|
mux.Handle("/dockerapi/", http.StripPrefix("/dockerapi", h))
|
||||||
mux.Handle("/", fileHandler)
|
mux.Handle("/", fileHandler)
|
||||||
return CSRF(csrfWrapper(mux))
|
return CSRF(csrfWrapper(mux))
|
||||||
|
|
Loading…
Reference in New Issue