2016-12-23 10:12:19 -05:00
|
|
|
/*
|
2018-02-06 18:07:17 -05:00
|
|
|
* Minio Cloud Storage, (C) 2016, 2017, 2018 Minio, Inc.
|
2016-12-23 10:12:19 -05: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2018-02-06 18:07:17 -05:00
|
|
|
"strconv"
|
2016-12-23 10:12:19 -05:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/minio/dsync"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Allow any RPC call request time should be no more/less than 3 seconds.
|
|
|
|
// 3 seconds is chosen arbitrarily.
|
|
|
|
const rpcSkewTimeAllowed = 3 * time.Second
|
|
|
|
|
|
|
|
func isRequestTimeAllowed(requestTime time.Time) bool {
|
|
|
|
// Check whether request time is within acceptable skew time.
|
2017-03-18 14:28:41 -04:00
|
|
|
utcNow := UTCNow()
|
2016-12-23 10:12:19 -05:00
|
|
|
return !(requestTime.Sub(utcNow) > rpcSkewTimeAllowed ||
|
|
|
|
utcNow.Sub(requestTime) > rpcSkewTimeAllowed)
|
|
|
|
}
|
|
|
|
|
2018-02-06 18:07:17 -05:00
|
|
|
// semVersion - RPC semantic versioning.
|
|
|
|
type semVersion struct {
|
|
|
|
Major uint64
|
|
|
|
Minor uint64
|
|
|
|
Patch uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
// semver comparator implementation based on the semver 2.0.0 https://semver.org/.
|
|
|
|
func (v semVersion) Compare(o semVersion) int {
|
|
|
|
if v.Major != o.Major {
|
|
|
|
if v.Major > o.Major {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
if v.Minor != o.Minor {
|
|
|
|
if v.Minor > o.Minor {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
if v.Patch != o.Patch {
|
|
|
|
if v.Patch > o.Patch {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v semVersion) String() string {
|
|
|
|
b := make([]byte, 0, 5)
|
|
|
|
b = strconv.AppendUint(b, v.Major, 10)
|
|
|
|
b = append(b, '.')
|
|
|
|
b = strconv.AppendUint(b, v.Minor, 10)
|
|
|
|
b = append(b, '.')
|
|
|
|
b = strconv.AppendUint(b, v.Patch, 10)
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
|
2016-12-23 10:12:19 -05:00
|
|
|
// AuthRPCArgs represents minimum required arguments to make any authenticated RPC call.
|
|
|
|
type AuthRPCArgs struct {
|
|
|
|
// Authentication token to be verified by the server for every RPC call.
|
|
|
|
AuthToken string
|
2018-02-06 18:07:17 -05:00
|
|
|
Version semVersion
|
2016-12-23 10:12:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetAuthToken - sets the token to the supplied value.
|
|
|
|
func (args *AuthRPCArgs) SetAuthToken(authToken string) {
|
|
|
|
args.AuthToken = authToken
|
|
|
|
}
|
|
|
|
|
2018-02-06 18:07:17 -05:00
|
|
|
// SetRPCAPIVersion - sets the rpc version to the supplied value.
|
|
|
|
func (args *AuthRPCArgs) SetRPCAPIVersion(version semVersion) {
|
|
|
|
args.Version = version
|
|
|
|
}
|
|
|
|
|
2016-12-23 10:12:19 -05:00
|
|
|
// IsAuthenticated - validated whether this auth RPC args are already authenticated or not.
|
|
|
|
func (args AuthRPCArgs) IsAuthenticated() error {
|
2018-02-06 18:07:17 -05:00
|
|
|
// checks if rpc Version is not equal to current server rpc version.
|
|
|
|
// this is fine for now, but in future when we add backward compatible
|
|
|
|
// APIs we need to make sure to allow lesser versioned clients to
|
|
|
|
// talk over RPC, until then we are fine with this check.
|
|
|
|
if args.Version.Compare(globalRPCAPIVersion) != 0 {
|
|
|
|
return errRPCAPIVersionUnsupported
|
|
|
|
}
|
|
|
|
|
2016-12-23 10:12:19 -05:00
|
|
|
// Check whether the token is valid
|
|
|
|
if !isAuthTokenValid(args.AuthToken) {
|
|
|
|
return errInvalidToken
|
|
|
|
}
|
|
|
|
|
|
|
|
// Good to go.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AuthRPCReply represents minimum required reply for any authenticated RPC call.
|
|
|
|
type AuthRPCReply struct{}
|
|
|
|
|
|
|
|
// LoginRPCArgs - login username and password for RPC.
|
|
|
|
type LoginRPCArgs struct {
|
2017-09-19 15:37:56 -04:00
|
|
|
AuthToken string
|
2018-02-06 18:07:17 -05:00
|
|
|
Version semVersion
|
2016-12-23 10:12:19 -05:00
|
|
|
RequestTime time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsValid - validates whether this LoginRPCArgs are valid for authentication.
|
|
|
|
func (args LoginRPCArgs) IsValid() error {
|
2018-02-06 18:07:17 -05:00
|
|
|
// checks if rpc Version is not equal to current server rpc version.
|
|
|
|
// this is fine for now, but in future when we add backward compatible
|
|
|
|
// APIs we need to make sure to allow lesser versioned clients to
|
|
|
|
// talk over RPC, until then we are fine with this check.
|
|
|
|
if args.Version.Compare(globalRPCAPIVersion) != 0 {
|
|
|
|
return errRPCAPIVersionUnsupported
|
2016-12-23 10:12:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if !isRequestTimeAllowed(args.RequestTime) {
|
|
|
|
return errServerTimeMismatch
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-19 15:37:56 -04:00
|
|
|
// LoginRPCReply - login reply is a dummy struct perhaps for future use.
|
|
|
|
type LoginRPCReply struct{}
|
2016-12-23 10:12:19 -05:00
|
|
|
|
|
|
|
// LockArgs represents arguments for any authenticated lock RPC call.
|
|
|
|
type LockArgs struct {
|
|
|
|
AuthRPCArgs
|
2017-01-08 23:37:53 -05:00
|
|
|
LockArgs dsync.LockArgs
|
2016-12-23 10:12:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func newLockArgs(args dsync.LockArgs) LockArgs {
|
2017-01-08 23:37:53 -05:00
|
|
|
return LockArgs{LockArgs: args}
|
2016-12-23 10:12:19 -05:00
|
|
|
}
|