2019-01-04 16:48:12 -05:00
|
|
|
// +build ignore
|
|
|
|
|
|
|
|
/*
|
2019-11-13 05:30:54 -05:00
|
|
|
* MinIO Cloud Storage, (C) 2019 MinIO, Inc.
|
2019-01-04 16:48:12 -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 main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/base64"
|
2019-11-30 00:37:42 -05:00
|
|
|
"encoding/json"
|
2019-01-04 16:48:12 -05:00
|
|
|
"encoding/xml"
|
2019-11-13 05:30:54 -05:00
|
|
|
"errors"
|
2019-01-04 16:48:12 -05:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/oauth2"
|
|
|
|
|
2019-11-13 05:30:54 -05:00
|
|
|
"github.com/minio/minio-go/v6"
|
|
|
|
"github.com/minio/minio-go/v6/pkg/credentials"
|
2019-01-04 16:48:12 -05:00
|
|
|
"github.com/minio/minio/pkg/auth"
|
|
|
|
)
|
|
|
|
|
|
|
|
// AssumedRoleUser - The identifiers for the temporary security credentials that
|
|
|
|
// the operation returns. Please also see https://docs.aws.amazon.com/goto/WebAPI/sts-2011-06-15/AssumedRoleUser
|
|
|
|
type AssumedRoleUser struct {
|
|
|
|
Arn string
|
|
|
|
AssumedRoleID string `xml:"AssumeRoleId"`
|
|
|
|
// contains filtered or unexported fields
|
|
|
|
}
|
|
|
|
|
|
|
|
// AssumeRoleWithWebIdentityResponse contains the result of successful AssumeRoleWithWebIdentity request.
|
|
|
|
type AssumeRoleWithWebIdentityResponse struct {
|
|
|
|
XMLName xml.Name `xml:"https://sts.amazonaws.com/doc/2011-06-15/ AssumeRoleWithWebIdentityResponse" json:"-"`
|
|
|
|
Result WebIdentityResult `xml:"AssumeRoleWithWebIdentityResult"`
|
|
|
|
ResponseMetadata struct {
|
|
|
|
RequestID string `xml:"RequestId,omitempty"`
|
|
|
|
} `xml:"ResponseMetadata,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// WebIdentityResult - Contains the response to a successful AssumeRoleWithWebIdentity
|
2019-04-09 14:39:42 -04:00
|
|
|
// request, including temporary credentials that can be used to make MinIO API requests.
|
2019-01-04 16:48:12 -05:00
|
|
|
type WebIdentityResult struct {
|
|
|
|
AssumedRoleUser AssumedRoleUser `xml:",omitempty"`
|
|
|
|
Audience string `xml:",omitempty"`
|
|
|
|
Credentials auth.Credentials `xml:",omitempty"`
|
|
|
|
PackedPolicySize int `xml:",omitempty"`
|
|
|
|
Provider string `xml:",omitempty"`
|
|
|
|
SubjectFromWebIdentityToken string `xml:",omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a base64 encoded random 32 byte string.
|
|
|
|
func randomState() string {
|
|
|
|
b := make([]byte, 32)
|
|
|
|
rand.Read(b)
|
|
|
|
return base64.RawURLEncoding.EncodeToString(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2019-11-30 00:37:42 -05:00
|
|
|
stsEndpoint string
|
|
|
|
configEndpoint string
|
|
|
|
clientID string
|
|
|
|
clientSec string
|
|
|
|
port int
|
2019-01-04 16:48:12 -05:00
|
|
|
)
|
|
|
|
|
2019-11-30 00:37:42 -05:00
|
|
|
// DiscoveryDoc - parses the output from openid-configuration
|
|
|
|
// for example https://accounts.google.com/.well-known/openid-configuration
|
|
|
|
type DiscoveryDoc struct {
|
|
|
|
Issuer string `json:"issuer,omitempty"`
|
|
|
|
AuthEndpoint string `json:"authorization_endpoint,omitempty"`
|
|
|
|
TokenEndpoint string `json:"token_endpoint,omitempty"`
|
|
|
|
UserInfoEndpoint string `json:"userinfo_endpoint,omitempty"`
|
|
|
|
RevocationEndpoint string `json:"revocation_endpoint,omitempty"`
|
|
|
|
JwksURI string `json:"jwks_uri,omitempty"`
|
|
|
|
ResponseTypesSupported []string `json:"response_types_supported,omitempty"`
|
|
|
|
SubjectTypesSupported []string `json:"subject_types_supported,omitempty"`
|
|
|
|
IDTokenSigningAlgValuesSupported []string `json:"id_token_signing_alg_values_supported,omitempty"`
|
|
|
|
ScopesSupported []string `json:"scopes_supported,omitempty"`
|
|
|
|
TokenEndpointAuthMethods []string `json:"token_endpoint_auth_methods_supported,omitempty"`
|
|
|
|
ClaimsSupported []string `json:"claims_supported,omitempty"`
|
|
|
|
CodeChallengeMethodsSupported []string `json:"code_challenge_methods_supported,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseDiscoveryDoc(ustr string) (DiscoveryDoc, error) {
|
|
|
|
d := DiscoveryDoc{}
|
|
|
|
req, err := http.NewRequest(http.MethodGet, ustr, nil)
|
|
|
|
if err != nil {
|
|
|
|
return d, err
|
|
|
|
}
|
|
|
|
clnt := http.Client{
|
|
|
|
Transport: http.DefaultTransport,
|
|
|
|
}
|
|
|
|
resp, err := clnt.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return d, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return d, err
|
|
|
|
}
|
|
|
|
dec := json.NewDecoder(resp.Body)
|
|
|
|
if err = dec.Decode(&d); err != nil {
|
|
|
|
return d, err
|
|
|
|
}
|
|
|
|
return d, nil
|
|
|
|
}
|
|
|
|
|
2019-01-04 16:48:12 -05:00
|
|
|
func init() {
|
|
|
|
flag.StringVar(&stsEndpoint, "sts-ep", "http://localhost:9000", "STS endpoint")
|
2019-11-30 00:37:42 -05:00
|
|
|
flag.StringVar(&configEndpoint, "config-ep",
|
|
|
|
"https://accounts.google.com/.well-known/openid-configuration",
|
|
|
|
"OpenID discovery document endpoint")
|
2019-01-04 16:48:12 -05:00
|
|
|
flag.StringVar(&clientID, "cid", "", "Client ID")
|
2019-11-30 00:37:42 -05:00
|
|
|
flag.StringVar(&clientSec, "csec", "", "Client Secret")
|
2019-10-23 01:59:13 -04:00
|
|
|
flag.IntVar(&port, "port", 8080, "Port")
|
2019-01-04 16:48:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
2019-11-30 00:37:42 -05:00
|
|
|
if clientID == "" || clientSec == "" {
|
2019-01-04 16:48:12 -05:00
|
|
|
flag.PrintDefaults()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-30 00:37:42 -05:00
|
|
|
ddoc, err := parseDiscoveryDoc(configEndpoint)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-01-04 16:48:12 -05:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
config := oauth2.Config{
|
|
|
|
ClientID: clientID,
|
2019-11-30 00:37:42 -05:00
|
|
|
ClientSecret: clientSec,
|
2019-01-04 16:48:12 -05:00
|
|
|
Endpoint: oauth2.Endpoint{
|
2019-11-30 00:37:42 -05:00
|
|
|
AuthURL: ddoc.AuthEndpoint,
|
|
|
|
TokenURL: ddoc.TokenEndpoint,
|
2019-01-04 16:48:12 -05:00
|
|
|
},
|
2019-11-30 00:37:42 -05:00
|
|
|
RedirectURL: fmt.Sprintf("http://localhost:%d/oauth2/callback", port),
|
2020-02-20 21:36:05 -05:00
|
|
|
Scopes: ddoc.ScopesSupported,
|
2019-01-04 16:48:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
state := randomState()
|
|
|
|
|
|
|
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
http.Redirect(w, r, config.AuthCodeURL(state), http.StatusFound)
|
|
|
|
})
|
|
|
|
|
|
|
|
http.HandleFunc("/oauth2/callback", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.URL.Query().Get("state") != state {
|
|
|
|
http.Error(w, "state did not match", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-13 05:30:54 -05:00
|
|
|
getWebTokenExpiry := func() (*credentials.WebIdentityToken, error) {
|
|
|
|
oauth2Token, err := config.Exchange(ctx, r.URL.Query().Get("code"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !oauth2Token.Valid() {
|
|
|
|
return nil, errors.New("invalid token")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &credentials.WebIdentityToken{
|
|
|
|
Token: oauth2Token.Extra("id_token").(string),
|
|
|
|
Expiry: int(oauth2Token.Expiry.Sub(time.Now().UTC()).Seconds()),
|
|
|
|
}, nil
|
|
|
|
}
|
2019-11-30 00:37:42 -05:00
|
|
|
|
2019-11-13 05:30:54 -05:00
|
|
|
sts, err := credentials.NewSTSWebIdentity(stsEndpoint, getWebTokenExpiry)
|
2019-01-04 16:48:12 -05:00
|
|
|
if err != nil {
|
2019-11-13 05:30:54 -05:00
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
2019-01-04 16:48:12 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-13 05:30:54 -05:00
|
|
|
// Uncomment this to use MinIO API operations by initializing minio
|
|
|
|
// client with obtained credentials.
|
2019-01-04 16:48:12 -05:00
|
|
|
|
2019-11-13 05:30:54 -05:00
|
|
|
opts := &minio.Options{
|
|
|
|
Creds: sts,
|
|
|
|
BucketLookup: minio.BucketLookupAuto,
|
|
|
|
}
|
2019-01-04 16:48:12 -05:00
|
|
|
|
2019-11-13 05:30:54 -05:00
|
|
|
u, err := url.Parse(stsEndpoint)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2019-01-04 16:48:12 -05:00
|
|
|
|
2019-11-13 05:30:54 -05:00
|
|
|
clnt, err := minio.NewWithOptions(u.Host, opts)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
buckets, err := clnt.ListBuckets()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, bucket := range buckets {
|
|
|
|
log.Println(bucket)
|
2019-01-04 16:48:12 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
address := fmt.Sprintf("localhost:%v", port)
|
|
|
|
log.Printf("listening on http://%s/", address)
|
|
|
|
log.Fatal(http.ListenAndServe(address, nil))
|
2019-01-04 16:48:12 -05:00
|
|
|
}
|