From da94ccfb0c5e94fd6f98b583692dc799b57ce5d5 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Sun, 18 Jan 2015 13:11:01 -0800 Subject: [PATCH] Implement 'configure' command and update description --- README.md | 2 +- api-options.go | 52 +++++++++++++++++++++++++++------------- common.go | 62 +++++++++++++++++++++++++++++------------------- configure.go | 53 +++++++++++++++++++++++++++++++++++++++++ fs-options.go | 1 - pkg/s3/client.go | 16 ++++++++----- 6 files changed, 138 insertions(+), 48 deletions(-) create mode 100644 configure.go diff --git a/README.md b/README.md index 8c50b4fd..000617af 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Minio Client -mc - unified command line interface for Minio Object Storage released under [Apache license v2](./LICENSE). +mc - unified command line interface for Minio Object Storage compatible with Amazon S3 API released under [Apache license v2](./LICENSE). # Install diff --git a/api-options.go b/api-options.go index 0762dc96..29720360 100644 --- a/api-options.go +++ b/api-options.go @@ -23,7 +23,7 @@ import ( var GetObject = cli.Command{ Name: "get-object", Usage: "", - Description: "", + Description: "Retrieves objects from Amazon S3.", Action: doGetObject, Flags: []cli.Flag{ cli.StringFlag{ @@ -42,7 +42,7 @@ var GetObject = cli.Command{ var PutObject = cli.Command{ Name: "put-object", Usage: "", - Description: "", + Description: "Adds an object to a bucket.", Action: doPutObject, Flags: []cli.Flag{ cli.StringFlag{ @@ -64,10 +64,12 @@ var PutObject = cli.Command{ } var ListObjects = cli.Command{ - Name: "list-objects", - Usage: "", - Description: "", - Action: doListObjects, + Name: "list-objects", + Usage: "", + Description: `Returns some or all (up to 1000) of the objects in a bucket. + You can use the request parameters as selection criteria to + return a subset of the objects in a bucket.`, + Action: doListObjects, Flags: []cli.Flag{ cli.StringFlag{ Name: "bucket", @@ -78,18 +80,36 @@ var ListObjects = cli.Command{ } var ListBuckets = cli.Command{ - Name: "list-buckets", - Usage: "", - Description: "", - Action: doListBuckets, + Name: "list-buckets", + Usage: "", + Description: `Returns a list of all buckets owned by the authenticated + sender of the request.`, + Action: doListBuckets, } var Configure = cli.Command{ - Name: "configure", - Usage: "", - Description: "", - Action: doConfigure, + Name: "configure", + Usage: "", + Description: `Configure minio client configuration data. If your config + file does not exist (the default location is ~/.s3auth), it will be + automatically created for you. Note that the configure command only writes + values to the config file. It does not use any configuration values from + the environment variables.`, + Action: doConfigure, + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "accesskey", + Value: "", + Usage: "AWS access key id", + }, + cli.StringFlag{ + Name: "secretkey", + Value: "", + Usage: "AWS secret key id", + }, + }, } -func doConfigure(c *cli.Context) { -} +const ( + S3_AUTH = ".s3auth" +) diff --git a/common.go b/common.go index f19e446c..196d3f84 100644 --- a/common.go +++ b/common.go @@ -17,22 +17,15 @@ package main import ( + "encoding/json" "errors" "os" + "path" "github.com/codegangsta/cli" + "github.com/minio-io/mc/pkg/s3" ) -type MinioClient struct { - bucketName string - keyName string - body string - bucketAcls string - policy string - region string - query string // TODO -} - var Options = []cli.Command{ Cp, Ls, @@ -48,21 +41,42 @@ var Options = []cli.Command{ Configure, } -func getAWSEnvironment() (accessKey, secretKey string, err error) { - accessKey = os.Getenv("AWS_ACCESS_KEY_ID") - secretKey = os.Getenv("AWS_SECRET_ACCESS_KEY") - if accessKey == "" && secretKey == "" { - errstr := `You can configure your credentials by running "mc configure"` - return "", "", errors.New(errstr) - } - if accessKey == "" { - errstr := `Partial credentials found in the env, missing : AWS_ACCESS_KEY_ID` - return "", "", errors.New(errstr) - } +func getAuthFilePath() string { + home := os.Getenv("HOME") + return path.Join(home, S3_AUTH) +} - if secretKey == "" { - errstr := `Partial credentials found in the env, missing : AWS_SECRET_ACCESS_KEY` - return "", "", errors.New(errstr) +func getAWSEnvironment() (accessKey, secretKey string, err error) { + var s3Auth *os.File + var auth s3.Auth + s3Auth, err = os.OpenFile(getAuthFilePath(), os.O_RDWR, 0666) + defer s3Auth.Close() + if err != nil { + accessKey = os.Getenv("AWS_ACCESS_KEY_ID") + secretKey = os.Getenv("AWS_SECRET_ACCESS_KEY") + if accessKey == "" && secretKey == "" { + errstr := `You can configure your credentials by running "mc configure"` + return "", "", errors.New(errstr) + } + if accessKey == "" { + errstr := `Partial credentials found in the env, missing : AWS_ACCESS_KEY_ID` + return "", "", errors.New(errstr) + } + + if secretKey == "" { + errstr := `Partial credentials found in the env, missing : AWS_SECRET_ACCESS_KEY` + return "", "", errors.New(errstr) + } + } else { + var n int + s3Authbytes := make([]byte, 256) + n, err = s3Auth.Read(s3Authbytes) + err = json.Unmarshal(s3Authbytes[:n], &auth) + if err != nil { + return "", "", err + } + accessKey = auth.AccessKey + secretKey = auth.SecretAccessKey } return accessKey, secretKey, nil diff --git a/configure.go b/configure.go new file mode 100644 index 00000000..11ad180d --- /dev/null +++ b/configure.go @@ -0,0 +1,53 @@ +package main + +import ( + "encoding/json" + "errors" + "log" + "os" + "path" + + "github.com/codegangsta/cli" + "github.com/minio-io/mc/pkg/s3" +) + +func parseConfigureInput(c *cli.Context) (accessKey, secretKey string, err error) { + accessKey = c.String("accesskey") + secretKey = c.String("secretkey") + if accessKey == "" { + return "", "", errors.New("accesskey is mandatory") + } + if secretKey == "" { + return "", "", errors.New("secretkey is mandatory") + } + return accessKey, secretKey, nil +} + +func doConfigure(c *cli.Context) { + var err error + var jAuth []byte + var accessKey, secretKey string + accessKey, secretKey, err = parseConfigureInput(c) + if err != nil { + log.Fatal(err) + } + + auth := s3.NewAuth(accessKey, secretKey, "s3.amazonaws.com") + jAuth, err = json.Marshal(auth) + if err != nil { + log.Fatal(err) + } + + var s3File *os.File + home := os.Getenv("HOME") + s3File, err = os.OpenFile(path.Join(home, S3_AUTH), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666) + defer s3File.Close() + if err != nil { + log.Fatal(err) + } + + _, err = s3File.Write(jAuth) + if err != nil { + log.Fatal(err) + } +} diff --git a/fs-options.go b/fs-options.go index 9a32c5c0..c2df9d7a 100644 --- a/fs-options.go +++ b/fs-options.go @@ -18,7 +18,6 @@ package main import ( "github.com/codegangsta/cli" - // "github.com/minio-io/mc/pkg/s3" ) var Cp = cli.Command{ diff --git a/pkg/s3/client.go b/pkg/s3/client.go index f8ad8271..81e0ec72 100644 --- a/pkg/s3/client.go +++ b/pkg/s3/client.go @@ -347,13 +347,17 @@ func (c *Client) Delete(bucket, key string) error { return fmt.Errorf("Amazon HTTP error on DELETE: %d", res.StatusCode) } +func NewAuth(accessKey, secretKey, hostname string) (auth *Auth) { + auth = &Auth{ + AccessKey: accessKey, + SecretAccessKey: secretKey, + Hostname: hostname} + return +} + func NewS3Client(accessKey, secretKey, hostname string) (client *Client) { - client = &Client{ - &Auth{ - AccessKey: accessKey, - SecretAccessKey: secretKey, - Hostname: hostname}, - http.DefaultTransport} + auth := NewAuth(accessKey, secretKey, hostname) + client = &Client{auth, http.DefaultTransport} return }