github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/command/testdata/login-oauth-server/main.go (about)

     1  // +build ignore
     2  
     3  // This file is a helper for those doing _manual_ testing of "terraform login"
     4  // and/or "terraform logout" and want to start up a test OAuth server in a
     5  // separate process for convenience:
     6  //
     7  //    go run ./command/testdata/login-oauth-server/main.go :8080
     8  //
     9  // This is _not_ the main way to use this oauthserver package. For automated
    10  // test code, import it as a normal Go package instead:
    11  //
    12  //     import oauthserver "github.com/hashicorp/terraform/command/testdata/login-oauth-server"
    13  
    14  package main
    15  
    16  import (
    17  	"fmt"
    18  	"net"
    19  	"net/http"
    20  	"os"
    21  
    22  	oauthserver "github.com/hashicorp/terraform/command/testdata/login-oauth-server"
    23  )
    24  
    25  func main() {
    26  	if len(os.Args) < 2 {
    27  		fmt.Fprintln(os.Stderr, "Usage: go run ./command/testdata/login-oauth-server/main.go <listen-address>")
    28  		os.Exit(1)
    29  	}
    30  
    31  	host, port, err := net.SplitHostPort(os.Args[1])
    32  	if err != nil {
    33  		fmt.Fprintln(os.Stderr, "Invalid address: %s", err)
    34  		os.Exit(1)
    35  	}
    36  
    37  	if host == "" {
    38  		host = "127.0.0.1"
    39  	}
    40  	addr := fmt.Sprintf("%s:%s", host, port)
    41  
    42  	fmt.Printf("Will listen on %s...\n", addr)
    43  	fmt.Printf(
    44  		configExampleFmt,
    45  		fmt.Sprintf("http://%s:%s/authz", host, port),
    46  		fmt.Sprintf("http://%s:%s/token", host, port),
    47  		fmt.Sprintf("http://%s:%s/revoke", host, port),
    48  	)
    49  
    50  	server := &http.Server{
    51  		Addr:    addr,
    52  		Handler: oauthserver.Handler,
    53  	}
    54  	err = server.ListenAndServe()
    55  	fmt.Fprintln(os.Stderr, err.Error())
    56  }
    57  
    58  const configExampleFmt = `
    59  host "login-test.example.com" {
    60    services = {
    61      "login.v1" = {
    62        authz       = %q
    63        token       = %q
    64        client      = "placeholder"
    65        grant_types = ["code", "password"]
    66      }
    67      "logout.v1" = %q
    68    }
    69  }
    70  
    71  `