Skip to content

Commit

Permalink
Use compression for the bridge config, to shrink it under 4096 charac…
Browse files Browse the repository at this point in the history
…ters..

This is to work around the Docker "exec" and "run -ti" terminal which breaks
when you paste more than 4096 characters.
  • Loading branch information
abourget committed May 9, 2017
1 parent da159aa commit 2d2d949
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
13 changes: 12 additions & 1 deletion cmd/serve.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"bytes"
"compress/gzip"
"crypto/tls"
"encoding/base64"
"encoding/json"
Expand Down Expand Up @@ -70,9 +72,18 @@ func serve(cmd *cobra.Command, args []string) {

jsonConfig, _ := json.Marshal(b)

bridgeConfText := base64.StdEncoding.EncodeToString(jsonConfig)
buf := &bytes.Buffer{}

gz, _ := gzip.NewWriterLevel(buf, gzip.BestCompression)
gz.Write([]byte(jsonConfig))
gz.Close()

configBytes := buf.Bytes()

bridgeConfText := base64.RawURLEncoding.EncodeToString(configBytes)
if writeConf {
log.Printf("Writing bridge conf to %q\n", confFile)

err = ioutil.WriteFile(confFile, []byte(bridgeConfText), 0600)
if err != nil {
log.Fatalf("Error writing %q: %s\n", confFile, err)
Expand Down
28 changes: 17 additions & 11 deletions pkg/bridge/auth.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package bridge

import (
"bytes"
"compress/gzip"
"crypto/tls"
"crypto/x509"
"encoding/base64"
Expand Down Expand Up @@ -41,20 +43,24 @@ func NewFromDefaultConfig() (bridge *Bridge, err error) {
}

func NewFromString(conf string) (bridge *Bridge, err error) {
var content []byte
content := []byte(strings.TrimSpace(conf))

conf = strings.TrimSpace(conf)
if !strings.HasPrefix(string(content), "{") {
content, err = base64.RawURLEncoding.DecodeString(conf)
if err != nil {
return nil, fmt.Errorf("decoding base64: %s", err)
}
}

if strings.HasPrefix(conf, "{") {
content = []byte(conf)
} else {
content, err = base64.StdEncoding.DecodeString(conf)
if !strings.HasPrefix(string(content), "{") {
// GZip became necessary when we hit a maximum of 4096 BYTES
// limitation on `docker exec` initiated terminals. You could
// never paste more than 4096 bytes in a swift.. By gzipping
// the JSON, we can shrink it under 4096 bytes.
gz, _ := gzip.NewReader(bytes.NewReader(content))
content, err = ioutil.ReadAll(gz)
if err != nil {
var err2 error
content, err2 = base64.RawStdEncoding.DecodeString(conf)
if err2 != nil {
return nil, fmt.Errorf("decoding base64 input: %s or %s", err2, err)
}
return nil, fmt.Errorf("gunzip: %s", err)
}
}

Expand Down

0 comments on commit 2d2d949

Please sign in to comment.