storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/docs/sts/ldap.go (about)

     1  //go:build ignore
     2  // +build ignore
     3  
     4  /*
     5   * MinIO Cloud Storage, (C) 2019 MinIO, Inc.
     6   *
     7   * Licensed under the Apache License, Version 2.0 (the "License");
     8   * you may not use this file except in compliance with the License.
     9   * You may obtain a copy of the License at
    10   *
    11   *     http://www.apache.org/licenses/LICENSE-2.0
    12   *
    13   * Unless required by applicable law or agreed to in writing, software
    14   * distributed under the License is distributed on an "AS IS" BASIS,
    15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16   * See the License for the specific language governing permissions and
    17   * limitations under the License.
    18   */
    19  package main
    20  
    21  import (
    22  	"context"
    23  	"flag"
    24  	"fmt"
    25  	"log"
    26  	"net/url"
    27  
    28  	"github.com/minio/minio-go/v7"
    29  	cr "github.com/minio/minio-go/v7/pkg/credentials"
    30  )
    31  
    32  var (
    33  	// LDAP integrated Minio endpoint
    34  	stsEndpoint string
    35  
    36  	// LDAP credentials
    37  	ldapUsername string
    38  	ldapPassword string
    39  )
    40  
    41  func init() {
    42  	flag.StringVar(&stsEndpoint, "sts-ep", "http://localhost:9000", "STS endpoint")
    43  	flag.StringVar(&ldapUsername, "u", "", "AD/LDAP Username")
    44  	flag.StringVar(&ldapPassword, "p", "", "AD/LDAP Password")
    45  }
    46  
    47  func main() {
    48  	flag.Parse()
    49  	if ldapUsername == "" || ldapPassword == "" {
    50  		flag.PrintDefaults()
    51  		return
    52  	}
    53  
    54  	// The credentials package in minio-go provides an interface to call the
    55  	// LDAP STS API.
    56  
    57  	// Initialize LDAP credentials
    58  	li, _ := cr.NewLDAPIdentity(stsEndpoint, ldapUsername, ldapPassword)
    59  
    60  	stsEndpointURL, err := url.Parse(stsEndpoint)
    61  	if err != nil {
    62  		log.Fatalf("Err: %v", err)
    63  	}
    64  
    65  	opts := &minio.Options{
    66  		Creds:  li,
    67  		Secure: stsEndpointURL.Scheme == "https",
    68  	}
    69  
    70  	fmt.Println(li.Get())
    71  	// Use generated credentials to authenticate with MinIO server
    72  	minioClient, err := minio.New(stsEndpointURL.Host, opts)
    73  	if err != nil {
    74  		log.Fatalln(err)
    75  	}
    76  
    77  	// Use minIO Client object normally like the regular client.
    78  	fmt.Println("Calling list objects with temp creds: ")
    79  	objCh := minioClient.ListObjects(context.Background(), ldapUsername, minio.ListObjectsOptions{})
    80  	for obj := range objCh {
    81  		if obj.Err != nil {
    82  			if err != nil {
    83  				log.Fatalln(err)
    84  			}
    85  		}
    86  		fmt.Println(obj)
    87  	}
    88  }