2016-08-12 06:33:55 +02:00
|
|
|
/*
|
2017-01-18 21:24:34 +01:00
|
|
|
* Minio Cloud Storage, (C) 2015, 2016, 2017 Minio, Inc.
|
2016-08-12 06:33:55 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-08-19 01:23:42 +02:00
|
|
|
package cmd
|
2016-08-12 06:33:55 +02:00
|
|
|
|
|
|
|
import (
|
2016-08-17 09:18:23 +02:00
|
|
|
"bufio"
|
2016-08-19 10:29:50 +02:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"crypto/x509/pkix"
|
|
|
|
"encoding/pem"
|
2016-08-12 06:33:55 +02:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2016-08-19 10:29:50 +02:00
|
|
|
"math/big"
|
2016-08-17 09:18:23 +02:00
|
|
|
"net"
|
2016-08-12 06:33:55 +02:00
|
|
|
"net/http"
|
2016-08-19 10:29:50 +02:00
|
|
|
"os"
|
2016-11-01 23:38:28 +01:00
|
|
|
"runtime"
|
2017-06-01 05:33:13 +02:00
|
|
|
"strings"
|
2016-08-19 10:29:50 +02:00
|
|
|
"sync"
|
2016-08-12 06:33:55 +02:00
|
|
|
"testing"
|
2016-08-19 04:45:03 +02:00
|
|
|
"time"
|
2016-08-12 06:33:55 +02:00
|
|
|
)
|
|
|
|
|
2016-11-05 17:43:28 +01:00
|
|
|
func TestListenerAcceptAfterClose(t *testing.T) {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for i := 0; i < 16; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
runTest(t)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func runTest(t *testing.T) {
|
|
|
|
const connectionsBeforeClose = 1
|
|
|
|
|
|
|
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-11-06 20:41:01 +01:00
|
|
|
|
|
|
|
ln = newListenerMux(ln, &tls.Config{})
|
2016-11-05 17:43:28 +01:00
|
|
|
|
|
|
|
addr := ln.Addr().String()
|
|
|
|
waitForListener := make(chan error)
|
|
|
|
go func() {
|
|
|
|
defer close(waitForListener)
|
|
|
|
|
|
|
|
var connCount int
|
|
|
|
for {
|
|
|
|
conn, aerr := ln.Accept()
|
|
|
|
if aerr != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
connCount++
|
|
|
|
if connCount > connectionsBeforeClose {
|
|
|
|
waitForListener <- errUnexpected
|
|
|
|
return
|
|
|
|
}
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for i := 0; i < connectionsBeforeClose; i++ {
|
|
|
|
err = dial(addr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ln.Close()
|
|
|
|
dial(addr)
|
|
|
|
|
|
|
|
err = <-waitForListener
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func dial(addr string) error {
|
|
|
|
conn, err := net.Dial("tcp", addr)
|
|
|
|
if err == nil {
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-11-10 16:44:41 +01:00
|
|
|
// Tests initializing listeners.
|
2016-11-01 23:38:28 +01:00
|
|
|
func TestInitListeners(t *testing.T) {
|
2017-01-17 02:05:00 +01:00
|
|
|
portTest1 := getFreePort()
|
|
|
|
portTest2 := getFreePort()
|
2016-11-01 23:38:28 +01:00
|
|
|
testCases := []struct {
|
|
|
|
serverAddr string
|
|
|
|
shouldPass bool
|
|
|
|
}{
|
|
|
|
// Test 1 with ip and port.
|
|
|
|
{
|
2017-01-17 02:05:00 +01:00
|
|
|
serverAddr: "127.0.0.1:" + portTest1,
|
2016-11-01 23:38:28 +01:00
|
|
|
shouldPass: true,
|
|
|
|
},
|
|
|
|
// Test 2 only port.
|
|
|
|
{
|
2017-01-17 02:05:00 +01:00
|
|
|
serverAddr: ":" + portTest2,
|
2016-11-01 23:38:28 +01:00
|
|
|
shouldPass: true,
|
|
|
|
},
|
|
|
|
// Test 3 with no port error.
|
|
|
|
{
|
|
|
|
serverAddr: "127.0.0.1",
|
|
|
|
shouldPass: false,
|
|
|
|
},
|
|
|
|
// Test 4 with 'foobar' host not resolvable.
|
|
|
|
{
|
|
|
|
serverAddr: "foobar:9000",
|
|
|
|
shouldPass: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for i, testCase := range testCases {
|
|
|
|
listeners, err := initListeners(testCase.serverAddr, &tls.Config{})
|
|
|
|
if testCase.shouldPass {
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Test %d: Unable to initialize listeners %s", i+1, err)
|
|
|
|
}
|
|
|
|
for _, listener := range listeners {
|
|
|
|
if err = listener.Close(); err != nil {
|
|
|
|
t.Fatalf("Test %d: Unable to close listeners %s", i+1, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err == nil && !testCase.shouldPass {
|
|
|
|
t.Fatalf("Test %d: Should fail but is successful", i+1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Windows doesn't have 'localhost' hostname.
|
2017-01-18 21:24:34 +01:00
|
|
|
if runtime.GOOS != globalWindowsOSName {
|
2016-11-01 23:38:28 +01:00
|
|
|
listeners, err := initListeners("localhost:"+getFreePort(), &tls.Config{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Test 3: Unable to initialize listeners %s", err)
|
|
|
|
}
|
|
|
|
for _, listener := range listeners {
|
|
|
|
if err = listener.Close(); err != nil {
|
|
|
|
t.Fatalf("Test 3: Unable to close listeners %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-12 06:33:55 +02:00
|
|
|
func TestClose(t *testing.T) {
|
|
|
|
// Create ServerMux
|
2016-09-03 04:07:42 +02:00
|
|
|
m := NewServerMux("", nil)
|
2016-08-12 06:33:55 +02:00
|
|
|
|
|
|
|
if err := m.Close(); err != nil {
|
|
|
|
t.Error("Server errored while trying to Close", err)
|
|
|
|
}
|
2016-11-01 23:38:28 +01:00
|
|
|
|
|
|
|
// Closing again should return an error.
|
|
|
|
if err := m.Close(); err.Error() != "Server has been closed" {
|
|
|
|
t.Error("Unexepcted error expected \"Server has been closed\", got", err)
|
|
|
|
}
|
2016-08-12 06:33:55 +02:00
|
|
|
}
|
|
|
|
|
2016-09-03 04:07:42 +02:00
|
|
|
func TestServerMux(t *testing.T) {
|
2017-02-04 07:53:30 +01:00
|
|
|
var err error
|
|
|
|
var got []byte
|
|
|
|
var res *http.Response
|
2016-08-12 06:33:55 +02:00
|
|
|
|
|
|
|
// Create ServerMux
|
2017-02-04 07:53:30 +01:00
|
|
|
m := NewServerMux("127.0.0.1:0", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2016-08-12 06:33:55 +02:00
|
|
|
fmt.Fprint(w, "hello")
|
|
|
|
}))
|
2017-02-04 07:53:30 +01:00
|
|
|
// Start serving requests
|
|
|
|
go m.ListenAndServe("", "")
|
|
|
|
|
|
|
|
// Issue a GET request. Since we started server in a goroutine, it could be not ready
|
|
|
|
// at this point. So we allow until 5 failed retries before declare there is an error
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
// Sleep one second
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
// Check if one listener is ready
|
|
|
|
m.mu.Lock()
|
2017-02-18 22:28:54 +01:00
|
|
|
listenersCount := len(m.listeners)
|
|
|
|
m.mu.Unlock()
|
|
|
|
if listenersCount == 0 {
|
2017-02-04 07:53:30 +01:00
|
|
|
continue
|
|
|
|
}
|
2017-02-18 22:28:54 +01:00
|
|
|
m.mu.Lock()
|
|
|
|
listenerAddr := m.listeners[0].Addr().String()
|
2017-02-04 07:53:30 +01:00
|
|
|
m.mu.Unlock()
|
|
|
|
// Issue the GET request
|
|
|
|
client := http.Client{}
|
2017-02-18 22:28:54 +01:00
|
|
|
res, err = client.Get("http://" + listenerAddr)
|
2017-02-04 07:53:30 +01:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Read the request response
|
|
|
|
got, err = ioutil.ReadAll(res.Body)
|
2017-02-18 22:28:54 +01:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// We've got a response, quit the loop
|
|
|
|
break
|
2016-08-12 06:33:55 +02:00
|
|
|
}
|
|
|
|
|
2017-02-04 07:53:30 +01:00
|
|
|
// Check for error persisted after 5 times
|
2016-08-12 06:33:55 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-02-04 07:53:30 +01:00
|
|
|
// Check the web service response
|
2016-08-12 06:33:55 +02:00
|
|
|
if string(got) != "hello" {
|
|
|
|
t.Errorf("got %q, want hello", string(got))
|
|
|
|
}
|
2016-08-17 09:18:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestServerCloseBlocking(t *testing.T) {
|
|
|
|
// Create ServerMux
|
2017-02-04 07:53:30 +01:00
|
|
|
m := NewServerMux("127.0.0.1:0", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2016-08-17 09:18:23 +02:00
|
|
|
fmt.Fprint(w, "hello")
|
|
|
|
}))
|
|
|
|
|
2017-02-04 07:53:30 +01:00
|
|
|
// Start serving requests in a goroutine
|
|
|
|
go m.ListenAndServe("", "")
|
|
|
|
|
|
|
|
// Dial, try until 5 times before declaring a failure
|
|
|
|
dial := func() (net.Conn, error) {
|
|
|
|
var c net.Conn
|
|
|
|
var err error
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
// Sleep one second in case of the server is not ready yet
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
// Check if there is at least one listener configured
|
|
|
|
m.mu.Lock()
|
|
|
|
if len(m.listeners) == 0 {
|
|
|
|
m.mu.Unlock()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
m.mu.Unlock()
|
|
|
|
// Run the actual Dial
|
|
|
|
m.mu.Lock()
|
|
|
|
c, err = net.Dial("tcp", m.listeners[0].Addr().String())
|
|
|
|
m.mu.Unlock()
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2016-08-17 09:18:23 +02:00
|
|
|
}
|
2017-02-04 07:53:30 +01:00
|
|
|
return c, err
|
2016-08-17 09:18:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Dial to open a StateNew but don't send anything
|
2017-02-04 07:53:30 +01:00
|
|
|
cnew, err := dial()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-08-17 09:18:23 +02:00
|
|
|
defer cnew.Close()
|
|
|
|
|
|
|
|
// Dial another connection but idle after a request to have StateIdle
|
2017-02-04 07:53:30 +01:00
|
|
|
cidle, err := dial()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-08-17 09:18:23 +02:00
|
|
|
defer cidle.Close()
|
2017-02-04 07:53:30 +01:00
|
|
|
|
2016-08-17 09:18:23 +02:00
|
|
|
cidle.Write([]byte("HEAD / HTTP/1.1\r\nHost: foo\r\n\r\n"))
|
2017-02-04 07:53:30 +01:00
|
|
|
_, err = http.ReadResponse(bufio.NewReader(cidle), nil)
|
2016-08-17 09:18:23 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we don't block forever.
|
|
|
|
m.Close()
|
|
|
|
}
|
2016-08-12 06:33:55 +02:00
|
|
|
|
2017-02-04 07:53:30 +01:00
|
|
|
func TestServerListenAndServePlain(t *testing.T) {
|
2016-08-19 04:45:03 +02:00
|
|
|
wait := make(chan struct{})
|
2016-10-27 12:30:52 +02:00
|
|
|
addr := net.JoinHostPort("127.0.0.1", getFreePort())
|
2016-08-17 09:18:23 +02:00
|
|
|
errc := make(chan error)
|
2016-08-19 10:29:50 +02:00
|
|
|
once := &sync.Once{}
|
2016-08-19 04:45:03 +02:00
|
|
|
|
2016-10-10 08:03:10 +02:00
|
|
|
// Initialize done channel specifically for each tests.
|
|
|
|
globalServiceDoneCh = make(chan struct{}, 1)
|
|
|
|
|
2016-08-19 04:45:03 +02:00
|
|
|
// Create ServerMux and when we receive a request we stop waiting
|
2016-09-03 04:07:42 +02:00
|
|
|
m := NewServerMux(addr, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2016-08-19 04:45:03 +02:00
|
|
|
fmt.Fprint(w, "hello")
|
2016-08-19 10:29:50 +02:00
|
|
|
once.Do(func() { close(wait) })
|
2016-08-19 04:45:03 +02:00
|
|
|
}))
|
|
|
|
|
|
|
|
// ListenAndServe in a goroutine, but we don't know when it's ready
|
2016-11-06 04:32:13 +01:00
|
|
|
go func() { errc <- m.ListenAndServe("", "") }()
|
2016-08-19 04:45:03 +02:00
|
|
|
|
2016-09-01 01:07:44 +02:00
|
|
|
wg := &sync.WaitGroup{}
|
|
|
|
wg.Add(1)
|
2016-08-19 04:45:03 +02:00
|
|
|
// Keep trying the server until it's accepting connections
|
2016-08-19 10:29:50 +02:00
|
|
|
go func() {
|
|
|
|
client := http.Client{Timeout: time.Millisecond * 10}
|
|
|
|
ok := false
|
|
|
|
for !ok {
|
|
|
|
res, _ := client.Get("http://" + addr)
|
|
|
|
if res != nil && res.StatusCode == http.StatusOK {
|
|
|
|
ok = true
|
|
|
|
}
|
2016-08-19 04:45:03 +02:00
|
|
|
}
|
|
|
|
|
2016-09-01 01:07:44 +02:00
|
|
|
wg.Done()
|
2016-08-19 10:29:50 +02:00
|
|
|
}()
|
2016-08-19 04:45:03 +02:00
|
|
|
|
2016-09-01 01:07:44 +02:00
|
|
|
wg.Wait()
|
|
|
|
|
2016-08-19 04:45:03 +02:00
|
|
|
// Block until we get an error or wait closed
|
2016-08-17 09:18:23 +02:00
|
|
|
select {
|
|
|
|
case err := <-errc:
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-08-19 04:45:03 +02:00
|
|
|
case <-wait:
|
|
|
|
m.Close() // Shutdown the ServerMux
|
2016-08-17 09:18:23 +02:00
|
|
|
return
|
|
|
|
}
|
2016-08-12 06:33:55 +02:00
|
|
|
}
|
2016-08-19 10:29:50 +02:00
|
|
|
|
2017-02-04 07:53:30 +01:00
|
|
|
func TestServerListenAndServeTLS(t *testing.T) {
|
2017-06-05 21:25:04 +02:00
|
|
|
rootPath, err := newTestConfig(globalMinioDefaultRegion)
|
2017-06-01 05:33:13 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Init Test config failed")
|
|
|
|
}
|
2017-06-05 21:25:04 +02:00
|
|
|
defer removeAll(rootPath)
|
2017-06-01 05:33:13 +02:00
|
|
|
|
2016-08-19 10:29:50 +02:00
|
|
|
wait := make(chan struct{})
|
2016-10-27 12:30:52 +02:00
|
|
|
addr := net.JoinHostPort("127.0.0.1", getFreePort())
|
2016-08-19 10:29:50 +02:00
|
|
|
errc := make(chan error)
|
|
|
|
once := &sync.Once{}
|
|
|
|
|
2016-10-10 08:03:10 +02:00
|
|
|
// Initialize done channel specifically for each tests.
|
|
|
|
globalServiceDoneCh = make(chan struct{}, 1)
|
|
|
|
|
2016-08-19 10:29:50 +02:00
|
|
|
// Create ServerMux and when we receive a request we stop waiting
|
2016-09-03 04:07:42 +02:00
|
|
|
m := NewServerMux(addr, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2016-08-19 10:29:50 +02:00
|
|
|
fmt.Fprint(w, "hello")
|
|
|
|
once.Do(func() { close(wait) })
|
|
|
|
}))
|
|
|
|
|
|
|
|
// Create a cert
|
2017-06-01 05:33:13 +02:00
|
|
|
err = createConfigDir()
|
2016-08-19 10:29:50 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-03-09 04:20:01 +01:00
|
|
|
certFile := getPublicCertFile()
|
|
|
|
keyFile := getPrivateKeyFile()
|
2016-08-19 10:29:50 +02:00
|
|
|
defer os.RemoveAll(certFile)
|
|
|
|
defer os.RemoveAll(keyFile)
|
|
|
|
|
|
|
|
err = generateTestCert(addr)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListenAndServe in a goroutine, but we don't know when it's ready
|
2016-11-06 04:32:13 +01:00
|
|
|
go func() { errc <- m.ListenAndServe(certFile, keyFile) }()
|
2016-08-19 10:29:50 +02:00
|
|
|
|
2016-09-01 01:07:44 +02:00
|
|
|
wg := &sync.WaitGroup{}
|
|
|
|
wg.Add(1)
|
2016-08-19 10:29:50 +02:00
|
|
|
go func() {
|
|
|
|
tr := &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
}
|
|
|
|
client := http.Client{
|
|
|
|
Timeout: time.Millisecond * 10,
|
|
|
|
Transport: tr,
|
|
|
|
}
|
2016-11-01 23:38:28 +01:00
|
|
|
okTLS := false
|
2017-06-01 05:33:13 +02:00
|
|
|
// Keep trying the server until it's accepting connections
|
2016-11-01 23:38:28 +01:00
|
|
|
for !okTLS {
|
2016-08-19 10:29:50 +02:00
|
|
|
res, _ := client.Get("https://" + addr)
|
|
|
|
if res != nil && res.StatusCode == http.StatusOK {
|
2016-11-01 23:38:28 +01:00
|
|
|
okTLS = true
|
2016-08-19 10:29:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-01 05:33:13 +02:00
|
|
|
// Once a request succeeds, subsequent requests should
|
|
|
|
// work fine.
|
|
|
|
res, err := client.Get("http://" + addr)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Got unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
// Without TLS we expect a Bad-Request response from the server.
|
|
|
|
if !(res != nil && res.StatusCode == http.StatusBadRequest && res.Request.URL.Scheme == httpScheme) {
|
|
|
|
t.Fatalf("Plaintext request to TLS server did not have expected response!")
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error reading body")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the expected error is received.
|
|
|
|
bodyStr := string(body)
|
|
|
|
apiErr := getAPIError(ErrInsecureClientRequest)
|
|
|
|
if !(strings.Contains(bodyStr, apiErr.Code) && strings.Contains(bodyStr, apiErr.Description)) {
|
|
|
|
t.Fatalf("Plaintext request to TLS server did not have expected response body!")
|
2016-11-01 23:38:28 +01:00
|
|
|
}
|
2016-09-01 01:07:44 +02:00
|
|
|
wg.Done()
|
2016-08-19 10:29:50 +02:00
|
|
|
}()
|
|
|
|
|
2016-09-01 01:07:44 +02:00
|
|
|
wg.Wait()
|
|
|
|
|
2016-08-19 10:29:50 +02:00
|
|
|
// Block until we get an error or wait closed
|
|
|
|
select {
|
|
|
|
case err := <-errc:
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case <-wait:
|
|
|
|
m.Close() // Shutdown the ServerMux
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// generateTestCert creates a cert and a key used for testing only
|
|
|
|
func generateTestCert(host string) error {
|
2017-03-09 04:20:01 +01:00
|
|
|
certPath := getPublicCertFile()
|
|
|
|
keyPath := getPrivateKeyFile()
|
2016-08-19 10:29:50 +02:00
|
|
|
priv, err := rsa.GenerateKey(rand.Reader, 2048)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
|
|
|
|
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
template := x509.Certificate{
|
|
|
|
SerialNumber: serialNumber,
|
|
|
|
Subject: pkix.Name{
|
|
|
|
Organization: []string{"Minio Test Cert"},
|
|
|
|
},
|
2017-03-18 19:28:41 +01:00
|
|
|
NotBefore: UTCNow(),
|
|
|
|
NotAfter: UTCNow().Add(time.Minute * 1),
|
2016-08-19 10:29:50 +02:00
|
|
|
|
|
|
|
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
|
|
|
|
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
|
|
|
BasicConstraintsValid: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
if ip := net.ParseIP(host); ip != nil {
|
|
|
|
template.IPAddresses = append(template.IPAddresses, ip)
|
|
|
|
}
|
|
|
|
|
|
|
|
template.IsCA = true
|
|
|
|
template.KeyUsage |= x509.KeyUsageCertSign
|
|
|
|
|
|
|
|
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
certOut, err := os.Create(certPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
|
|
|
|
certOut.Close()
|
|
|
|
|
|
|
|
keyOut, err := os.OpenFile(keyPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)})
|
|
|
|
keyOut.Close()
|
|
|
|
return nil
|
|
|
|
}
|