Minio client now has SSL support

This commit is contained in:
Harshavardhana
2015-01-29 15:11:41 -08:00
parent a0f5031396
commit ed0a7e3c40
10 changed files with 170 additions and 29 deletions
+28 -9
View File
@@ -78,19 +78,38 @@ func getMinioAuthFilePath() string {
return path.Join(home, MINIO_AUTH)
}
// TODO reply full json
func getMinioEnvironment() (hostname string, err error) {
var auth *minio.Auth
func getMinioEnvironment() (auth *minio.Auth, err error) {
var accessKey, secretKey string
minioAuth, err := os.OpenFile(getMinioAuthFilePath(), os.O_RDWR, 0666)
defer minioAuth.Close()
var n int
minioAuthbytes := make([]byte, 256)
n, err = minioAuth.Read(minioAuthbytes)
err = json.Unmarshal(minioAuthbytes[:n], &auth)
if err != nil {
return "", err
accessKey = os.Getenv("MINIO_ACCESS_KEY_ID")
secretKey = os.Getenv("MINIO_SECRET_ACCESS_KEY")
if accessKey == "" && secretKey == "" {
return nil, missingAccessSecretErr
}
if accessKey == "" {
return nil, missingAccessErr
}
if secretKey == "" {
return nil, missingSecretErr
}
auth = &minio.Auth{
AccessKey: accessKey,
SecretAccessKey: secretKey,
Hostname: "127.0.0.1:8080",
}
} else {
var n int
minioAuthbytes := make([]byte, 1024)
n, err = minioAuth.Read(minioAuthbytes)
err = json.Unmarshal(minioAuthbytes[:n], &auth)
if err != nil {
return nil, err
}
}
return auth.Hostname, nil
return auth, nil
}
func getAWSEnvironment() (auth *s3.Auth, err error) {
+33 -1
View File
@@ -28,7 +28,39 @@ func doMinioConfigure(c *cli.Context) {
if hostname == "" {
log.Fatal("Invalid hostname")
}
auth := &minio.Auth{"", "", hostname}
caFile := c.String("cacert")
if caFile == "" {
log.Fatal("invalid CA file")
}
certFile := c.String("cert")
if certFile == "" {
log.Fatal("invalid certificate")
}
keyFile := c.String("key")
if keyFile == "" {
log.Fatal("invalid key")
}
/*
var accessKey, secretKey string
var err error
accessKey, secretKey, err = parseConfigureInput(c)
if err != nil {
log.Fatal(err)
}
*/
auth := &minio.Auth{
// AccessKey: accessKey,
// SecretAccessKey: secretKey,
Hostname: hostname,
CACert: caFile,
CertPEM: certFile,
KeyPEM: keyFile,
}
jAuth, err := json.Marshal(auth)
if err != nil {
log.Fatal(err)
+25
View File
@@ -116,6 +116,31 @@ var MinioConfigure = cli.Command{
Value: "127.0.0.1:8080",
Usage: "Minio object server",
},
cli.StringFlag{
Name: "accesskey",
Value: "",
Usage: "Minio access key",
},
cli.StringFlag{
Name: "SecretKey",
Value: "",
Usage: "Minio secret key",
},
cli.StringFlag{
Name: "cacert",
Value: "",
Usage: "CA authority cert",
},
cli.StringFlag{
Name: "cert",
Value: "",
Usage: "Minio server certificate",
},
cli.StringFlag{
Name: "key",
Value: "",
Usage: "Minio server private key",
},
},
}
+4 -3
View File
@@ -26,12 +26,13 @@ import (
)
func minioGetObject(c *cli.Context) {
var bucket, key, hostname string
var bucket, key string
var err error
var objectReader io.ReadCloser
var objectSize int64
var auth *minio.Auth
hostname, err = getMinioEnvironment()
auth, err = getMinioEnvironment()
if err != nil {
log.Fatal(err)
}
@@ -45,7 +46,7 @@ func minioGetObject(c *cli.Context) {
log.Fatal(objectNameErr)
}
minio := minio.NewMinioClient(hostname)
minio, _ := minio.NewMinioClient(auth)
objectReader, objectSize, err = minio.Get(bucket, key)
if err != nil {
log.Fatal(err)
+2 -2
View File
@@ -14,13 +14,13 @@ func minioDumpBuckets(v []*minio.Bucket) {
}
func minioListBuckets(c *cli.Context) {
hostname, err := getMinioEnvironment()
auth, err := getMinioEnvironment()
if err != nil {
log.Fatal(err)
}
var buckets []*minio.Bucket
mc := minio.NewMinioClient(hostname)
mc, _ := minio.NewMinioClient(auth)
buckets, err = mc.Buckets()
if err != nil {
log.Fatal(err)
+4 -3
View File
@@ -33,9 +33,10 @@ func minioParseListObjectsInput(c *cli.Context) (bucket string, err error) {
func minioListObjects(c *cli.Context) {
var err error
var bucket, hostname string
var bucket string
var auth *minio.Auth
hostname, err = getMinioEnvironment()
auth, err = getMinioEnvironment()
if err != nil {
log.Fatal(err)
}
@@ -46,7 +47,7 @@ func minioListObjects(c *cli.Context) {
}
var items []*minio.Item
mc := minio.NewMinioClient(hostname)
mc, _ := minio.NewMinioClient(auth)
// Gets 1000 maxkeys supported with GET Bucket API
items, err = mc.GetBucket(bucket, "", minio.MAX_OBJECT_LIST)
if err != nil {
+4 -3
View File
@@ -24,10 +24,11 @@ import (
)
func minioPutBucket(c *cli.Context) {
var bucket, hostname string
var bucket string
var auth *minio.Auth
var err error
hostname, err = getMinioEnvironment()
auth, err = getMinioEnvironment()
if err != nil {
log.Fatal(err)
}
@@ -37,7 +38,7 @@ func minioPutBucket(c *cli.Context) {
log.Fatal(bucketNameErr)
}
mc := minio.NewMinioClient(hostname)
mc, _ := minio.NewMinioClient(auth)
err = mc.PutBucket(bucket)
if err != nil {
log.Fatal(err)
+4 -3
View File
@@ -72,8 +72,9 @@ func minioParsePutObjectInput(c *cli.Context) (bucket, key, body string, err err
func minioPutObject(c *cli.Context) {
var err error
var bucket, key, body, hostname string
hostname, err = getMinioEnvironment()
var bucket, key, body string
var auth *minio.Auth
auth, err = getMinioEnvironment()
if err != nil {
log.Fatal(err)
}
@@ -82,7 +83,7 @@ func minioPutObject(c *cli.Context) {
if err != nil {
log.Fatal(err)
}
mc := minio.NewMinioClient(hostname)
mc, _ := minio.NewMinioClient(auth)
var bodyFile *os.File
bodyFile, err = os.Open(body)
if err != nil {
+57
View File
@@ -1,7 +1,64 @@
package minio
import (
"crypto/tls"
"crypto/x509"
"io/ioutil"
"net/http"
)
type Auth struct {
AccessKey string
SecretAccessKey string
Hostname string
CACert string
CertPEM string
KeyPEM string
}
type TlsConfig struct {
CABlock []byte
CertPEMBlock []byte
KeyPEMBlock []byte
}
func (a *Auth) loadKeys(cert string, key string) (*TlsConfig, error) {
caBlock, err := ioutil.ReadFile(cert)
if err != nil {
return nil, err
}
keyBlock, err := ioutil.ReadFile(key)
if err != nil {
return nil, err
}
t := &TlsConfig{}
t.CABlock = caBlock
t.CertPEMBlock = caBlock
t.KeyPEMBlock = keyBlock
return t, nil
}
func (a *Auth) getTlsTransport() (*http.Transport, error) {
tlsconfig, err := a.loadKeys(a.CACert, a.KeyPEM)
if err != nil {
return nil, err
}
var cert tls.Certificate
cert, err = tls.X509KeyPair(tlsconfig.CertPEMBlock, tlsconfig.KeyPEMBlock)
if err != nil {
return nil, err
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(tlsconfig.CABlock)
// Setup HTTPS client
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{cert},
RootCAs: caCertPool,
}
tlsConfig.BuildNameToCertificate()
transport := &http.Transport{TLSClientConfig: tlsConfig}
return transport, nil
}
+9 -5
View File
@@ -67,7 +67,7 @@ func (c *Client) hostname() string {
// bucketURL returns the URL prefix of the bucket, with trailing slash
func (c *Client) bucketURL(bucket string) string {
return fmt.Sprintf("http://%s/%s/", c.hostname(), bucket)
return fmt.Sprintf("https://%s/%s/", c.hostname(), bucket)
}
func (c *Client) keyURL(bucket, key string) string {
@@ -84,7 +84,7 @@ func newReq(url_ string) *http.Request {
}
func (c *Client) Buckets() ([]*Bucket, error) {
req := newReq("http://" + c.hostname() + "/")
req := newReq("https://" + c.hostname() + "/")
res, err := c.transport().RoundTrip(req)
if err != nil {
return nil, err
@@ -325,9 +325,13 @@ func (c *Client) GetPartial(bucket, key string, offset, length int64) (rc io.Rea
}
}
func NewMinioClient(hostname string) (client *Client) {
client = &Client{hostname, http.DefaultTransport}
return
func NewMinioClient(auth *Auth) (client *Client, err error) {
transport, err := auth.getTlsTransport()
if err != nil {
return nil, err
}
client = &Client{auth.Hostname, transport}
return client, nil
}
// Error is the type returned by some API operations.