Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set up TLS for TestDefaultClientRejectSelfSigned #11

Merged
merged 3 commits into from
Dec 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ _cgo_gotypes.go
_cgo_export.*

_testmain.go
*.test

*.exe

# IntelliJ files
.idea
*.iml
*.iml
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ gocommon

Common Go library for Joyent's Triton and Manta.

[![wercker status](https://app.wercker.com/status/2f63bf7f68bfdd46b979abad19c0bee0/s/master "wercker status")](https://app.wercker.com/project/byKey/2f63bf7f68bfdd46b979abad19c0bee0)

## Installation

Use `go-get` to install gocommon.
Expand Down Expand Up @@ -70,7 +72,20 @@ upstream [email protected]:joyent/gocommon.git (push)

### Run Tests

The library needs values for the `SDC_URL`, `MANTA_URL`, `MANTA_KEY_ID` and `SDC_KEY_ID` environment variables even though the tests are run locally. You can generate a temporary key and use its fingerprint for tests without adding the key to your Triton Cloud account.

```
# create a temporary key
ssh-keygen -b 2048 -C "Testing Key" -f /tmp/id_rsa -t rsa -P ""

# set up environment
# note: leave the -E md5 argument off on older ssh-keygen
export KEY_ID=$(ssh-keygen -E md5 -lf /tmp/id_rsa | awk -F' ' '{print $2}' | cut -d':' -f2-)
export SDC_KEY_ID=${KEY_ID}
export MANTA_KEY_ID=${KEY_ID}
export SDC_URL=https://us-east-1.api.joyent.com
export MANTA_URL=https://us-east.manta.joyent.com

cd ${GOPATH}/src/github.com/joyent/gocommon
go test ./...
```
Expand Down
2 changes: 1 addition & 1 deletion errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const (
RequestTooLargeError = Code("RequestTooLarge")
RequestMovedError = Code("RequestMoved")
ResourceNotFoundError = Code("ResourceNotFound")
UnknownErrorError = Code("UnkownError")
UnknownErrorError = Code("UnknownError")
)

// Error instances store an optional error cause.
Expand Down
13 changes: 10 additions & 3 deletions http/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

httpsuite "github.com/joyent/gocommon/testing"
"github.com/joyent/gosign/auth"
"github.com/julienschmidt/httprouter"
)

const (
Expand Down Expand Up @@ -66,7 +67,7 @@ func (s *LoopingHTTPSuite) SetUpSuite(c *gc.C) {
func (s *LoopingHTTPSuite) setupLoopbackRequest() (*http.Header, chan string, *Client) {
var headers http.Header
bodyChan := make(chan string, 1)
handler := func(resp http.ResponseWriter, req *http.Request) {
handler := func(resp http.ResponseWriter, req *http.Request, _ httprouter.Params) {
headers = req.Header
bodyBytes, _ := ioutil.ReadAll(req.Body)
req.Body.Close()
Expand All @@ -75,7 +76,7 @@ func (s *LoopingHTTPSuite) setupLoopbackRequest() (*http.Header, chan string, *C
resp.WriteHeader(http.StatusNoContent)
resp.Write([]byte{})
}
s.Mux.HandleFunc("/", handler)
s.Mux.POST("/", handler)
client := New(s.creds, "", nil)

return &headers, bodyChan, client
Expand All @@ -89,8 +90,14 @@ type HTTPSClientTestSuite struct {
LoopingHTTPSuite
}

func newTLSsuite() *HTTPSClientTestSuite {
suite := &HTTPSClientTestSuite{}
suite.UseTLS = true
return suite
}

var _ = gc.Suite(&HTTPClientTestSuite{})
var _ = gc.Suite(&HTTPSClientTestSuite{})
var _ = gc.Suite(newTLSsuite())

func (s *HTTPClientTestSuite) assertHeaderValues(c *gc.C, apiVersion string) {
emptyHeaders := http.Header{}
Expand Down
9 changes: 4 additions & 5 deletions testing/httpsuite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"testing"

jt "github.com/joyent/gocommon/testing"
"github.com/julienschmidt/httprouter"
)

type HTTPTestSuite struct {
Expand All @@ -28,16 +29,14 @@ func Test(t *testing.T) {
var _ = gc.Suite(&HTTPTestSuite{})
var _ = gc.Suite(&HTTPSTestSuite{jt.HTTPSuite{UseTLS: true}})

type HelloHandler struct{}

func (h *HelloHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func HelloHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(200)
w.Write([]byte("Hello World\n"))
}

func (s *HTTPTestSuite) TestHelloWorld(c *gc.C) {
s.Mux.Handle("/", &HelloHandler{})
s.Mux.GET("/", HelloHandler)
response, err := http.Get(s.Server.URL)
c.Check(err, gc.IsNil)
content, err := ioutil.ReadAll(response.Body)
Expand All @@ -49,7 +48,7 @@ func (s *HTTPTestSuite) TestHelloWorld(c *gc.C) {
}

func (s *HTTPSTestSuite) TestHelloWorldWithTLS(c *gc.C) {
s.Mux.Handle("/", &HelloHandler{})
s.Mux.GET("/", HelloHandler)
c.Check(s.Server.URL[:8], gc.Equals, "https://")
response, err := http.Get(s.Server.URL)
// Default http.Get fails because the cert is self-signed
Expand Down
40 changes: 40 additions & 0 deletions wercker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
box: golang

build:
steps:
# Sets the go workspace and places you package
# at the right place in the workspace tree
- setup-go-workspace:
package-dir: github.com/joyent/gocommon

# Gets the dependencies
- script:
name: go get
code: |
go get -v -t ./...

# Build the project
- script:
name: go build
code: |
go build ./...

- script:
name: make a new key for testing
code: |
ssh-keygen -b 2048 \
-C "Testing Key" \
-f /root/.ssh/id_rsa \
-t rsa \
-P ""

# Test the project
- script:
name: go test
code: |
export KEY_ID=$(ssh-keygen -lf /root/.ssh/id_rsa | awk -F' ' '{print $2}' | cut -d':' -f2-)
export SDC_KEY_ID=${KEY_ID}
export MANTA_KEY_ID=${KEY_ID}
export SDC_URL=https://us-east-1.api.joyent.com
export MANTA_URL=https://us-east.manta.joyent.com
go test ./...