Add auth rpc service to generate access keys, add corresponding test

This commit is contained in:
Harshavardhana
2015-07-08 14:33:54 -07:00
parent 770fd23afa
commit 396b728031
8 changed files with 118 additions and 18 deletions

View File

@@ -25,8 +25,8 @@ import (
"strings"
"time"
"github.com/minio/minio/pkg/auth"
"github.com/minio/minio/pkg/quick"
"github.com/minio/minio/pkg/server/api/auth/keys"
)
type contentTypeHandler struct {
@@ -45,7 +45,7 @@ type resourceHandler struct {
handler http.Handler
}
type auth struct {
type authHeader struct {
prefix string
credential string
signedheaders string
@@ -62,13 +62,13 @@ const (
)
// strip auth from authorization header
func stripAuth(r *http.Request) (*auth, error) {
authHeader := r.Header.Get("Authorization")
if authHeader == "" {
func stripAuth(r *http.Request) (*authHeader, error) {
ah := r.Header.Get("Authorization")
if ah == "" {
return nil, errors.New("Missing auth header")
}
a := new(auth)
authFields := strings.Split(authHeader, ",")
a := new(authHeader)
authFields := strings.Split(ah, ",")
if len(authFields) != 3 {
return nil, errors.New("Missing fields in Auth header")
}
@@ -95,7 +95,7 @@ func stripAuth(r *http.Request) (*auth, error) {
a.signedheaders = signedheaders[1]
a.signature = signature[1]
a.accessKey = strings.Split(a.credential, "/")[0]
if !keys.IsValidAccessKey(a.accessKey) {
if !auth.IsValidAccessKey(a.accessKey) {
return nil, errors.New("Invalid access key")
}
return a, nil

View File

@@ -117,6 +117,7 @@ func getRPCHandler() http.Handler {
s.RegisterService(new(rpc.MemStatsService), "MemStats")
s.RegisterService(new(rpc.DiskInfoService), "DiskInfo")
s.RegisterService(new(rpc.DonutService), "Donut")
s.RegisterService(new(rpc.AuthService), "Auth")
// Add new RPC services here
return registerRPC(router.NewRouter(), s)
}

View File

@@ -0,0 +1,52 @@
/*
* Minimalist Object Storage, (C) 2015 Minio, Inc.
*
* 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.
*/
package rpc
import (
"net/http"
"github.com/minio/minio/pkg/auth"
"github.com/minio/minio/pkg/iodine"
)
// AuthService auth service
type AuthService struct{}
// AuthReply reply with new access keys and secret ids
type AuthReply struct {
AccessKeyID string `json:"accesskey"`
SecretAccessKey string `json:"secretaccesskey"`
}
func getAuth(reply *AuthReply) error {
accessID, err := auth.GenerateAccessKeyID()
if err != nil {
return iodine.New(err, nil)
}
reply.AccessKeyID = string(accessID)
secretID, err := auth.GenerateSecretAccessKey()
if err != nil {
return iodine.New(err, nil)
}
reply.SecretAccessKey = string(secretID)
return nil
}
// Get auth keys
func (s *AuthService) Get(r *http.Request, args *Args, reply *AuthReply) error {
return getAuth(reply)
}

View File

@@ -99,3 +99,24 @@ func (s *MyRPCSuite) TestSysInfo(c *C) {
resp.Body.Close()
c.Assert(reply, Not(DeepEquals), rpc.SysInfoReply{})
}
func (s *MyRPCSuite) TestAuth(c *C) {
op := controller.RPCOps{
Method: "Auth.Get",
Request: rpc.Args{Request: ""},
}
req, err := controller.NewRequest(testRPCServer.URL+"/rpc", op, http.DefaultTransport)
c.Assert(err, IsNil)
c.Assert(req.Get("Content-Type"), Equals, "application/json")
resp, err := req.Do()
c.Assert(err, IsNil)
c.Assert(resp.StatusCode, Equals, http.StatusOK)
var reply rpc.AuthReply
err = jsonrpc.DecodeClientResponse(resp.Body, &reply)
c.Assert(err, IsNil)
resp.Body.Close()
c.Assert(reply, Not(DeepEquals), rpc.AuthReply{})
c.Assert(len(reply.AccessKeyID), Equals, 20)
c.Assert(len(reply.SecretAccessKey), Equals, 40)
}