openfga

package module
v0.44.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 7, 2026 License: MIT Imports: 6 Imported by: 2

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type OpenFGAContainer

type OpenFGAContainer struct {
	testcontainers.Container
}

OpenFGAContainer represents the OpenFGA container type used in the module

func Run added in v0.32.0

Run creates an instance of the OpenFGA container type

Example
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/testcontainers/testcontainers-go"
	"github.com/testcontainers/testcontainers-go/modules/openfga"
)

func main() {
	// runOpenFGAContainer {
	ctx := context.Background()

	openfgaContainer, err := openfga.Run(ctx, "openfga/openfga:v1.18.0")
	defer func() {
		if err := testcontainers.TerminateContainer(openfgaContainer); err != nil {
			log.Printf("failed to terminate container: %s", err)
		}
	}()
	if err != nil {
		log.Printf("failed to start container: %s", err)
		return
	}
	// }

	state, err := openfgaContainer.State(ctx)
	if err != nil {
		log.Printf("failed to get container state: %s", err)
		return
	}

	fmt.Println(state.Running)

}
Output:
true
Example (ConnectToPlayground)
package main

import (
	"context"
	"fmt"
	"log"
	"net/http"

	"github.com/testcontainers/testcontainers-go"
	"github.com/testcontainers/testcontainers-go/modules/openfga"
	"github.com/testcontainers/testcontainers-go/wait"
)

func main() {
	// enablePlayground {
	openfgaContainer, err := openfga.Run(
		context.Background(),
		"openfga/openfga:v1.18.0",
		testcontainers.WithEnv(map[string]string{
			"OPENFGA_PLAYGROUND_ENABLED": "true",
			"OPENFGA_PLAYGROUND_ADDR":    "0.0.0.0:3000",
		}),
		testcontainers.WithAdditionalWaitStrategy(
			wait.ForHTTP("/playground").WithPort("3000/tcp").WithStatusCodeMatcher(func(status int) bool {
				return status == http.StatusOK
			})),
	)
	// }
	defer func() {
		if err := testcontainers.TerminateContainer(openfgaContainer); err != nil {
			log.Printf("failed to terminate container: %s", err)
		}
	}()
	if err != nil {
		log.Printf("failed to start container: %s", err)
		return
	}

	// playgroundEndpoint {
	playgroundEndpoint, err := openfgaContainer.PlaygroundEndpoint(context.Background())
	if err != nil {
		log.Printf("failed to get playground endpoint: %s", err)
		return
	}
	// }

	httpClient := http.Client{}

	resp, err := httpClient.Get(playgroundEndpoint)
	if err != nil {
		log.Printf("failed to get playground endpoint: %s", err)
		return
	}

	fmt.Println(resp.StatusCode)

}
Output:
200
Example (ConnectWithSDKClient)
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/openfga/go-sdk/client"

	"github.com/testcontainers/testcontainers-go"
	"github.com/testcontainers/testcontainers-go/modules/openfga"
)

func main() {
	openfgaContainer, err := openfga.Run(context.Background(), "openfga/openfga:v1.18.0")
	defer func() {
		if err := testcontainers.TerminateContainer(openfgaContainer); err != nil {
			log.Printf("failed to terminate container: %s", err)
		}
	}()
	if err != nil {
		log.Printf("failed to start container: %s", err)
		return
	}

	// httpEndpoint {
	httpEndpoint, err := openfgaContainer.HttpEndpoint(context.Background())
	if err != nil {
		log.Printf("failed to get HTTP endpoint: %s", err)
		return
	}
	// }

	// StoreId is not required for listing and creating stores
	fgaClient, err := client.NewSdkClient(&client.ClientConfiguration{
		ApiUrl: httpEndpoint, // required
	})
	if err != nil {
		log.Printf("failed to create SDK client: %s", err)
		return
	}

	list, err := fgaClient.ListStores(context.Background()).Execute()
	if err != nil {
		log.Printf("failed to list stores: %s", err)
		return
	}

	fmt.Println(len(list.Stores))

	store, err := fgaClient.CreateStore(context.Background()).Body(client.ClientCreateStoreRequest{Name: "test"}).Execute()
	if err != nil {
		log.Printf("failed to create store: %s", err)
		return
	}

	fmt.Println(store.Name)

	list, err = fgaClient.ListStores(context.Background()).Execute()
	if err != nil {
		log.Printf("failed to list stores: %s", err)
		return
	}

	fmt.Println(len(list.Stores))

}
Output:
0
test
1
Example (WriteModel)
package main

import (
	"context"
	"encoding/json"
	"fmt"
	"io"
	"log"
	"os"
	"path/filepath"

	"github.com/openfga/go-sdk/client"
	"github.com/openfga/go-sdk/credentials"

	"github.com/testcontainers/testcontainers-go"
	"github.com/testcontainers/testcontainers-go/modules/openfga"
)

func main() {
	// openFGAwriteModel {
	secret := "openfga-secret"
	openfgaContainer, err := openfga.Run(
		context.Background(),
		"openfga/openfga:v1.18.0",
		testcontainers.WithEnv(map[string]string{
			"OPENFGA_LOG_LEVEL":            "warn",
			"OPENFGA_AUTHN_METHOD":         "preshared",
			"OPENFGA_AUTHN_PRESHARED_KEYS": secret,
		}),
	)
	defer func() {
		if err := testcontainers.TerminateContainer(openfgaContainer); err != nil {
			log.Printf("failed to terminate container: %s", err)
		}
	}()
	if err != nil {
		log.Printf("failed to start container: %s", err)
		return
	}

	httpEndpoint, err := openfgaContainer.HttpEndpoint(context.Background())
	if err != nil {
		log.Printf("failed to get HTTP endpoint: %s", err)
		return
	}

	fgaClient, err := client.NewSdkClient(&client.ClientConfiguration{
		ApiUrl: httpEndpoint,
		Credentials: &credentials.Credentials{
			Method: credentials.CredentialsMethodApiToken,
			Config: &credentials.Config{
				ApiToken: secret,
			},
		},
		// because we are going to write an authorization model,
		// we need to specify a store id. Else, it will fail with
		// "Configuration.StoreId is required and must be specified to call this method"
		// In this example, it's an arbitrary store id, that will be created
		// on the fly.
		StoreId: "11111111111111111111111111",
	})
	if err != nil {
		log.Printf("failed to create openfga client: %v", err)
		return
	}

	f, err := os.Open(filepath.Join("testdata", "authorization_model.json"))
	if err != nil {
		log.Printf("failed to open file: %v", err)
		return
	}
	defer f.Close()

	bs, err := io.ReadAll(f)
	if err != nil {
		log.Printf("failed to read file: %v", err)
		return
	}

	var body client.ClientWriteAuthorizationModelRequest
	if err := json.Unmarshal(bs, &body); err != nil {
		log.Printf("failed to unmarshal json: %v", err)
		return
	}

	resp, err := fgaClient.WriteAuthorizationModel(context.Background()).Body(body).Execute()
	if err != nil {
		log.Printf("failed to write authorization model: %v", err)
		return
	}

	// }

	value, ok := resp.GetAuthorizationModelIdOk()
	fmt.Println(ok)
	fmt.Println(*value != "")

}
Output:
true
true

func RunContainer deprecated

Deprecated: use Run instead RunContainer creates an instance of the OpenFGA container type

func (*OpenFGAContainer) GrpcEndpoint

func (c *OpenFGAContainer) GrpcEndpoint(ctx context.Context) (string, error)

GrpcEndpoint returns the gRPC endpoint for the OpenFGA container, which uses the 8081/tcp port.

func (*OpenFGAContainer) HttpEndpoint

func (c *OpenFGAContainer) HttpEndpoint(ctx context.Context) (string, error)

HttpEndpoint returns the HTTP endpoint for the OpenFGA container, which uses the 8080/tcp port.

func (*OpenFGAContainer) PlaygroundEndpoint

func (c *OpenFGAContainer) PlaygroundEndpoint(ctx context.Context) (string, error)

PlaygroundEndpoint returns the playground endpoint for the OpenFGA container, which is the HTTP endpoint with the path /playground in the port 3000/tcp.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL