github.com/jcmturner/gokrb5/v8@v8.4.4/examples/longRunningClient.go (about)

     1  //go:build examples
     2  // +build examples
     3  
     4  package main
     5  
     6  import (
     7  	"encoding/hex"
     8  	"log"
     9  	"os"
    10  	"time"
    11  
    12  	"github.com/jcmturner/gokrb5/v8/client"
    13  	"github.com/jcmturner/gokrb5/v8/config"
    14  	"github.com/jcmturner/gokrb5/v8/keytab"
    15  	"github.com/jcmturner/gokrb5/v8/test/testdata"
    16  )
    17  
    18  const (
    19  	kRB5CONF = `[libdefaults]
    20    default_realm = TEST.GOKRB5
    21    dns_lookup_realm = false
    22    dns_lookup_kdc = false
    23    ticket_lifetime = 24h
    24    forwardable = yes
    25    default_tkt_enctypes = aes256-cts-hmac-sha1-96
    26    default_tgs_enctypes = aes256-cts-hmac-sha1-96
    27  
    28  [realms]
    29   TEST.GOKRB5 = {
    30    kdc = 10.80.88.88:88
    31    admin_server = 10.80.88.88:749
    32    default_domain = test.gokrb5
    33   }
    34  
    35  [domain_realm]
    36   .test.gokrb5 = TEST.GOKRB5
    37   test.gokrb5 = TEST.GOKRB5
    38   `
    39  )
    40  
    41  func main() {
    42  	l := log.New(os.Stderr, "GOKRB5 Client: ", log.LstdFlags)
    43  
    44  	//defer profile.Start(profile.TraceProfile).Stop()
    45  	// Load the keytab
    46  	kb, _ := hex.DecodeString(testdata.KEYTAB_TESTUSER2_TEST_GOKRB5)
    47  	kt := keytab.New()
    48  	err := kt.Unmarshal(kb)
    49  	if err != nil {
    50  		l.Fatalf("could not load client keytab: %v", err)
    51  	}
    52  
    53  	// Load the client krb5 config
    54  	conf, err := config.NewFromString(kRB5CONF)
    55  	if err != nil {
    56  		l.Fatalf("could not load krb5.conf: %v", err)
    57  	}
    58  	addr := os.Getenv("TEST_KDC_ADDR")
    59  	if addr != "" {
    60  		conf.Realms[0].KDC = []string{addr + ":88"}
    61  	}
    62  
    63  	// Create the client with the keytab
    64  	cl := client.NewWithKeytab("testuser2", "TEST.GOKRB5", kt, conf, client.Logger(l), client.DisablePAFXFAST(true))
    65  
    66  	// Log in the client
    67  	err = cl.Login()
    68  	if err != nil {
    69  		l.Fatalf("could not login client: %v", err)
    70  	}
    71  
    72  	for {
    73  		_, _, err := cl.GetServiceTicket("HTTP/host.test.gokrb5")
    74  		if err != nil {
    75  			l.Printf("failed to get service ticket: %v\n", err)
    76  		}
    77  		time.Sleep(time.Minute * 5)
    78  	}
    79  }