2021-08-18 21:35:22 -04:00
|
|
|
//go:build ignore
|
2019-09-09 19:12:29 -04:00
|
|
|
// +build ignore
|
|
|
|
|
2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2019-09-09 19:12:29 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-09-01 02:56:22 -04:00
|
|
|
"context"
|
2019-09-23 18:21:16 -04:00
|
|
|
"flag"
|
2019-09-09 19:12:29 -04:00
|
|
|
"fmt"
|
2021-07-15 18:27:34 -04:00
|
|
|
"io/ioutil"
|
2019-09-09 19:12:29 -04:00
|
|
|
"log"
|
2020-04-24 01:14:45 -04:00
|
|
|
"net/url"
|
2021-07-15 18:27:34 -04:00
|
|
|
"os"
|
2021-07-22 19:43:57 -04:00
|
|
|
"time"
|
2019-09-09 19:12:29 -04:00
|
|
|
|
2020-09-01 02:56:22 -04:00
|
|
|
"github.com/minio/minio-go/v7"
|
2020-07-14 12:38:05 -04:00
|
|
|
cr "github.com/minio/minio-go/v7/pkg/credentials"
|
2019-09-09 19:12:29 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// LDAP integrated Minio endpoint
|
2019-09-23 18:21:16 -04:00
|
|
|
stsEndpoint string
|
2019-09-09 19:12:29 -04:00
|
|
|
|
|
|
|
// LDAP credentials
|
2019-09-23 18:21:16 -04:00
|
|
|
ldapUsername string
|
|
|
|
ldapPassword string
|
2021-07-15 18:27:34 -04:00
|
|
|
|
|
|
|
// Display credentials flag
|
|
|
|
displayCreds bool
|
|
|
|
|
2021-07-22 19:43:57 -04:00
|
|
|
// Credential expiry duration
|
|
|
|
expiryDuration time.Duration
|
|
|
|
|
2021-07-15 18:27:34 -04:00
|
|
|
// Bucket to list
|
|
|
|
bucketToList string
|
|
|
|
|
|
|
|
// Session policy file
|
|
|
|
sessionPolicyFile string
|
2019-09-09 19:12:29 -04:00
|
|
|
)
|
|
|
|
|
2019-09-23 18:21:16 -04:00
|
|
|
func init() {
|
|
|
|
flag.StringVar(&stsEndpoint, "sts-ep", "http://localhost:9000", "STS endpoint")
|
|
|
|
flag.StringVar(&ldapUsername, "u", "", "AD/LDAP Username")
|
|
|
|
flag.StringVar(&ldapPassword, "p", "", "AD/LDAP Password")
|
2021-07-15 18:27:34 -04:00
|
|
|
flag.BoolVar(&displayCreds, "d", false, "Only show generated credentials")
|
2021-07-22 19:43:57 -04:00
|
|
|
flag.DurationVar(&expiryDuration, "e", 0, "Request a duration of validity for the generated credential")
|
2021-07-15 18:27:34 -04:00
|
|
|
flag.StringVar(&bucketToList, "b", "", "Bucket to list (defaults to ldap username)")
|
|
|
|
flag.StringVar(&sessionPolicyFile, "s", "", "File containing session policy to apply to the STS request")
|
2019-09-23 18:21:16 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 19:12:29 -04:00
|
|
|
func main() {
|
2019-09-23 18:21:16 -04:00
|
|
|
flag.Parse()
|
|
|
|
if ldapUsername == "" || ldapPassword == "" {
|
|
|
|
flag.PrintDefaults()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-17 16:29:31 -04:00
|
|
|
// The credentials package in minio-go provides an interface to call the
|
|
|
|
// LDAP STS API.
|
2019-09-09 19:12:29 -04:00
|
|
|
|
2019-09-17 16:29:31 -04:00
|
|
|
// Initialize LDAP credentials
|
2021-07-22 19:43:57 -04:00
|
|
|
var ldapOpts []cr.LDAPIdentityOpt
|
|
|
|
if sessionPolicyFile != "" {
|
2021-07-15 18:27:34 -04:00
|
|
|
var policy string
|
|
|
|
if f, err := os.Open(sessionPolicyFile); err != nil {
|
|
|
|
log.Fatalf("Unable to open session policy file: %v", sessionPolicyFile, err)
|
|
|
|
} else {
|
|
|
|
bs, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error reading session policy file: %v", err)
|
|
|
|
}
|
|
|
|
policy = string(bs)
|
|
|
|
}
|
2021-07-22 19:43:57 -04:00
|
|
|
ldapOpts = append(ldapOpts, cr.LDAPIdentityPolicyOpt(policy))
|
|
|
|
}
|
|
|
|
if expiryDuration != 0 {
|
|
|
|
ldapOpts = append(ldapOpts, cr.LDAPIdentityExpiryOpt(expiryDuration))
|
2021-07-15 18:27:34 -04:00
|
|
|
}
|
2021-07-22 19:43:57 -04:00
|
|
|
li, err := cr.NewLDAPIdentity(stsEndpoint, ldapUsername, ldapPassword, ldapOpts...)
|
2021-07-15 18:27:34 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error initializing LDAP Identity: %v", err)
|
|
|
|
}
|
2019-09-09 19:12:29 -04:00
|
|
|
|
2020-09-01 02:56:22 -04:00
|
|
|
stsEndpointURL, err := url.Parse(stsEndpoint)
|
2020-04-24 01:14:45 -04:00
|
|
|
if err != nil {
|
2021-07-15 18:27:34 -04:00
|
|
|
log.Fatalf("Error parsing sts endpoint: %v", err)
|
2020-04-24 01:14:45 -04:00
|
|
|
}
|
|
|
|
|
2020-09-01 02:56:22 -04:00
|
|
|
opts := &minio.Options{
|
|
|
|
Creds: li,
|
|
|
|
Secure: stsEndpointURL.Scheme == "https",
|
2020-04-24 01:14:45 -04:00
|
|
|
}
|
|
|
|
|
2021-07-15 18:27:34 -04:00
|
|
|
v, err := li.Get()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error retrieving STS credentials: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if displayCreds {
|
|
|
|
fmt.Println("Only displaying credentials:")
|
|
|
|
fmt.Println("AccessKeyID:", v.AccessKeyID)
|
|
|
|
fmt.Println("SecretAccessKey:", v.SecretAccessKey)
|
|
|
|
fmt.Println("SessionToken:", v.SessionToken)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-17 16:29:31 -04:00
|
|
|
// Use generated credentials to authenticate with MinIO server
|
2020-09-01 02:56:22 -04:00
|
|
|
minioClient, err := minio.New(stsEndpointURL.Host, opts)
|
2019-09-09 19:12:29 -04:00
|
|
|
if err != nil {
|
2021-07-15 18:27:34 -04:00
|
|
|
log.Fatalf("Error initializing client: ", err)
|
2019-09-09 19:12:29 -04:00
|
|
|
}
|
|
|
|
|
2019-09-17 16:29:31 -04:00
|
|
|
// Use minIO Client object normally like the regular client.
|
2021-07-15 18:27:34 -04:00
|
|
|
if bucketToList == "" {
|
|
|
|
bucketToList = ldapUsername
|
|
|
|
}
|
|
|
|
fmt.Printf("Calling list objects on bucket named `%s` with temp creds:\n===\n", bucketToList)
|
|
|
|
objCh := minioClient.ListObjects(context.Background(), bucketToList, minio.ListObjectsOptions{})
|
2020-09-01 02:56:22 -04:00
|
|
|
for obj := range objCh {
|
|
|
|
if obj.Err != nil {
|
2021-07-15 18:27:34 -04:00
|
|
|
log.Fatalf("Listing error: %v", obj.Err)
|
2020-09-01 02:56:22 -04:00
|
|
|
}
|
2021-07-15 18:27:34 -04:00
|
|
|
fmt.Printf("Key: %s\nSize: %d\nLast Modified: %s\n===\n", obj.Key, obj.Size, obj.LastModified)
|
2019-09-09 19:12:29 -04:00
|
|
|
}
|
|
|
|
}
|