storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/madmin/examples/service-accounts.go (about)

     1  //go:build ignore
     2  // +build ignore
     3  
     4  /*
     5   * MinIO Cloud Storage, (C) 2020 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   */
    20  
    21  package main
    22  
    23  import (
    24  	"context"
    25  	"fmt"
    26  	"log"
    27  
    28  	"storj.io/minio/pkg/bucket/policy"
    29  	"storj.io/minio/pkg/bucket/policy/condition"
    30  	iampolicy "storj.io/minio/pkg/iam/policy"
    31  	"storj.io/minio/pkg/madmin"
    32  )
    33  
    34  func main() {
    35  	// Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY are
    36  	// dummy values, please replace them with original values.
    37  
    38  	// Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY are
    39  	// dummy values, please replace them with original values.
    40  
    41  	// API requests are secure (HTTPS) if secure=true and insecure (HTTP) otherwise.
    42  	// New returns an MinIO Admin client object.
    43  	madmClnt, err := madmin.New("your-minio.example.com:9000", "YOUR-ACCESSKEYID", "YOUR-SECRETACCESSKEY", true)
    44  	if err != nil {
    45  		log.Fatalln(err)
    46  	}
    47  
    48  	p := iampolicy.Policy{
    49  		Version: iampolicy.DefaultVersion,
    50  		Statements: []iampolicy.Statement{
    51  			iampolicy.NewStatement(
    52  				policy.Allow,
    53  				iampolicy.NewActionSet(iampolicy.GetObjectAction),
    54  				iampolicy.NewResourceSet(iampolicy.NewResource("testbucket/*", "")),
    55  				condition.NewFunctions(),
    56  			)},
    57  	}
    58  
    59  	// Create a new service account
    60  	creds, err := madmClnt.AddServiceAccount(context.Background(), madmin.AddServiceAccountReq{Policy: &p})
    61  	if err != nil {
    62  		log.Fatalln(err)
    63  	}
    64  	fmt.Println(creds)
    65  
    66  	// List all services accounts
    67  	list, err := madmClnt.ListServiceAccounts(context.Background(), "")
    68  	if err != nil {
    69  		log.Fatalln(err)
    70  	}
    71  	fmt.Println(list)
    72  
    73  	// Delete a service account
    74  	err = madmClnt.DeleteServiceAccount(context.Background(), list.Accounts[0])
    75  	if err != nil {
    76  		log.Fatalln(err)
    77  	}
    78  }