/
auth-handler.go
162 lines (144 loc) · 5.22 KB
/
auth-handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package api
import (
"net"
"net/http"
"regexp"
"strings"
"github.com/journeymidnight/yig/api/datatype/policy"
. "github.com/journeymidnight/yig/error"
"github.com/journeymidnight/yig/helper"
"github.com/journeymidnight/yig/iam/common"
meta "github.com/journeymidnight/yig/meta/types"
"github.com/journeymidnight/yig/signature"
)
// Check request auth type verifies the incoming http request
// - validates the request signature
// - validates the policy action if anonymous tests bucket policies if any,
// for authenticated requests validates IAM policies.
// returns APIErrorCode if any to be replied to the client.
func checkRequestAuth(r *http.Request, action policy.Action) (c common.Credential, err error) {
// TODO:Location constraint
ctx := getRequestContext(r)
logger := ctx.Logger
authType := ctx.AuthType
switch authType {
case signature.AuthTypeUnknown:
logger.Info("ErrAccessDenied: AuthTypeUnknown")
return c, ErrSignatureVersionNotSupported
case signature.AuthTypeSignedV4, signature.AuthTypePresignedV4,
signature.AuthTypePresignedV2, signature.AuthTypeSignedV2:
helper.Logger.Info("AuthTypeSigned:", authType)
if c, err := signature.IsReqAuthenticated(r); err != nil {
helper.Logger.Info("ErrAccessDenied: IsReqAuthenticated return false:", err)
return c, err
} else {
helper.Logger.Info("Credential:", c)
// check bucket policy
isAllow, err := IsBucketPolicyAllowed(c.UserId, ctx.BucketInfo, r, action, ctx.ObjectName)
c.AllowOtherUserAccess = isAllow
return c, err
}
case signature.AuthTypeAnonymous:
isAllow, err := IsBucketPolicyAllowed(c.UserId, ctx.BucketInfo, r, action, ctx.ObjectName)
c.AllowOtherUserAccess = isAllow
return c, err
}
return c, ErrAccessDenied
}
func IsBucketPolicyAllowed(userId string, bucket *meta.Bucket, r *http.Request, action policy.Action, objectName string) (allow bool, err error) {
if bucket == nil {
return false, ErrAccessDenied
}
if bucket.OwnerId == userId {
return false, nil
}
policyResult := bucket.Policy.IsAllowed(policy.Args{
// TODO: Add IAM policy. Current account name is always useless.
AccountName: userId,
Action: action,
BucketName: bucket.Name,
ConditionValues: getConditionValues(r, ""),
IsOwner: false,
ObjectName: objectName,
})
if policyResult == policy.PolicyAllow {
return true, nil
} else if policyResult == policy.PolicyDeny {
return false, ErrAccessDenied
} else {
return false, nil
}
}
func getConditionValues(request *http.Request, locationConstraint string) map[string][]string {
args := make(map[string][]string)
for key, values := range request.Header {
if existingValues, found := args[key]; found {
args[key] = append(existingValues, values...)
} else {
args[key] = values
}
}
for key, values := range request.URL.Query() {
if existingValues, found := args[key]; found {
args[key] = append(existingValues, values...)
} else {
args[key] = values
}
}
args["SourceIp"] = []string{GetSourceIP(request)}
if locationConstraint != "" {
args["LocationConstraint"] = []string{locationConstraint}
}
return args
}
var (
// De-facto standard header keys.
xForwardedFor = http.CanonicalHeaderKey("X-Forwarded-For")
xRealIP = http.CanonicalHeaderKey("X-Real-IP")
// RFC7239 defines a new "Forwarded: " header designed to replace the
// existing use of X-Forwarded-* headers.
// e.g. Forwarded: for=192.0.2.60;proto=https;by=203.0.113.43
forwarded = http.CanonicalHeaderKey("Forwarded")
// Allows for a sub-match of the first value after 'for=' to the next
// comma, semi-colon or space. The match is case-insensitive.
forRegex = regexp.MustCompile(`(?i)(?:for=)([^(;|,| )]+)(.*)`)
// Allows for a sub-match for the first instance of scheme (http|https)
// prefixed by 'proto='. The match is case-insensitive.
)
// GetSourceIP retrieves the IP from the X-Forwarded-For, X-Real-IP and RFC7239
// Forwarded headers (in that order), falls back to r.RemoteAddr when all
// else fails.
func GetSourceIP(r *http.Request) string {
var addr string
if fwd := r.Header.Get(xForwardedFor); fwd != "" {
// Only grab the first (client) address. Note that '192.168.0.1,
// 10.1.1.1' is a valid key for X-Forwarded-For where addresses after
// the first may represent forwarding proxies earlier in the chain.
s := strings.Index(fwd, ", ")
if s == -1 {
s = len(fwd)
}
addr = fwd[:s]
} else if fwd := r.Header.Get(xRealIP); fwd != "" {
// X-Real-IP should only contain one IP address (the client making the
// request).
addr = fwd
} else if fwd := r.Header.Get(forwarded); fwd != "" {
// match should contain at least two elements if the protocol was
// specified in the Forwarded header. The first element will always be
// the 'for=' capture, which we ignore. In the case of multiple IP
// addresses (for=8.8.8.8, 8.8.4.4, 172.16.1.20 is valid) we only
// extract the first, which should be the client IP.
if match := forRegex.FindStringSubmatch(fwd); len(match) > 1 {
// IPv6 addresses in Forwarded headers are quoted-strings. We strip
// these quotes.
addr = strings.Trim(match[1], `"`)
}
}
if addr != "" {
return addr
}
// Default to remote address if headers not set.
addr, _, _ = net.SplitHostPort(r.RemoteAddr)
return addr
}