fmc

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2025 License: MPL-2.0 Imports: 17 Imported by: 0

README

Tests

go-fmc

go-fmc is a Go client library for Cisco Secure FMC (Firewall Management Center) and cdFMC (Cloud-Delivered FMC). It is based on Nathan's excellent goaci module and features a simple, extensible API and advanced JSON manipulation.

Getting Started

Installing

To start using go-fmc, install Go and go get:

$ go get -u github.com/netascode/go-fmc

Basic Usage
Self-managed FMC
package main

import "github.com/netascode/go-fmc"

func main() {
    client, _ := fmc.NewClient("https://1.1.1.1", "user", "pwd")

    res, _ := client.Get("/api/fmc_config/v1/domain/{DOMAIN_UUID}/object/networks")
    println(res.Get("items.0.name").String())
}
Cloud-managed FMC
package main

import "github.com/netascode/go-fmc"

func main() {
    client, _ := fmc.NewClientCDFMC("https://<YOUR_TENNANT_URL>.cdo.cisco.com", "apiToken")

    res, _ := client.Get("/api/fmc_config/v1/domain/{DOMAIN_UUID}/object/networks")
    println(res.Get("items.0.name").String())
}
Output

Both of those examples will print something like:

any-ipv4
Result manipulation

fmc.Result uses GJSON to simplify handling JSON results. See the GJSON documentation for more detail.

res, _ := client.Get("/api/fmc_config/v1/domain/{DOMAIN_UUID}/object/networks")

for _, obj := range res.Get("items").Array() {
    println(obj.Get("@pretty").String()) // pretty print network objects
}
POST data creation

fmc.Body is a wrapper for SJSON. SJSON supports a path syntax simplifying JSON creation.

body := fmc.Body{}.
    Set("name", "net1").
    Set("value", "1.5.4.0/24")
client.Post("/api/fmc_config/v1/domain/{DOMAIN_UUID}/object/networks", body.Str)

Documentation

See the documentation for more details.

Documentation

Overview

Package fmc is a Cisco Secure FMC (Firewall Management Center) REST client library for Go.

Index

Constants

View Source
const DefaultBackoffDelayFactor float64 = 3
View Source
const DefaultBackoffMaxDelay int = 60
View Source
const DefaultBackoffMinDelay int = 2
View Source
const DefaultMaxRetries int = 3

Variables

This section is empty.

Functions

func BackoffDelayFactor

func BackoffDelayFactor(x float64) func(*Client)

BackoffDelayFactor modifies the backoff delay factor from the default of 3.

func BackoffMaxDelay

func BackoffMaxDelay(x int) func(*Client)

BackoffMaxDelay modifies the maximum delay between two retries from the default of 60.

func BackoffMinDelay

func BackoffMinDelay(x int) func(*Client)

BackoffMinDelay modifies the minimum delay between two retries from the default of 2.

func CustomHttpClient

func CustomHttpClient(httpClient *http.Client) func(*Client)

Replace the default HTTP client with a custom one.

func DomainName

func DomainName(x string) func(*Req)

DomainName modifies the domain to be used for the request.

func Insecure

func Insecure(x bool) func(*Client)

Insecure determines if insecure https connections are allowed. Default value is true.

func MaxRetries

func MaxRetries(x int) func(*Client)

MaxRetries modifies the maximum number of retries from the default of 3.

func NoLogPayload

func NoLogPayload(req *Req)

NoLogPayload prevents logging of payloads. Primarily used by the Login and Refresh methods where this could expose secrets.

func RequestID added in v0.3.0

func RequestID(x string) func(*Req)

Set request ID

func RequestTimeout

func RequestTimeout(x time.Duration) func(*Client)

RequestTimeout modifies the HTTP request timeout from the default of 60 seconds.

func UserAgent added in v0.2.0

func UserAgent(x string) func(*Client)

UserAgent modifies the HTTP user agent string. Default value is 'go-meraki netascode'.

Types

type Body

type Body struct {
	Str string
}

Body wraps SJSON for building JSON body strings. Usage example:

Body{}.Set("name", "ABC").Str

func (Body) Delete

func (body Body) Delete(path string) Body

Delete deletes a JSON path.

func (Body) Res

func (body Body) Res() Res

Res creates a Res object, i.e. a GJSON result object.

func (Body) Set

func (body Body) Set(path string, value interface{}) Body

Set sets a JSON path to a value.

func (Body) SetRaw

func (body Body) SetRaw(path, rawValue string) Body

SetRaw sets a JSON path to a raw string value. This is primarily used for building up nested structures, e.g.:

Body{}.SetRaw("children", Body{}.Set("name", "New").Str).Str

type Client

type Client struct {
	// HttpClient is the *http.Client used for API requests.
	HttpClient *http.Client
	// Url is the FMC IP or hostname, e.g. https://10.0.0.1:443 (port is optional).
	Url string

	// UserAgent is the HTTP User-Agent string
	UserAgent string
	// Usr is the FMC username. Not used for cdFMC.
	Usr string
	// Pwd is the FMC password or cdFMC API token
	Pwd string
	// Insecure determines if insecure https connections are allowed.
	Insecure bool
	// Maximum number of retries
	MaxRetries int
	// Minimum delay between two retries
	BackoffMinDelay int
	// Maximum delay between two retries
	BackoffMaxDelay int
	// Backoff delay factor
	BackoffDelayFactor float64
	// LastRefresh is the timestamp of the last authentication token refresh
	LastRefresh time.Time
	// RefreshCount is the number to authentication token refreshes with the same refresh token
	RefreshCount int
	// DomainUUID is the UUID of the user login domain.
	DomainUUID string
	// Map of domain names to domain UUIDs.
	Domains map[string]string
	// FMC Version string as returned by FMC - ex. 7.7.0 (build 91)
	FMCVersion string
	// FMC Version parsed to go-version library - ex. 7.7.0
	FMCVersionParsed *version.Version
	// Is this cdFMC connection
	IsCDFMC bool
	// Rate limit requests to FMC
	RateLimiterBucket *ratelimit.Bucket
	// contains filtered or unexported fields
}

Client is an HTTP FMC client. Use fmc.NewClient to initiate a client. This will ensure proper cookie handling and processing of modifiers.

Requests are protected from concurrent writing (concurrent DELETE/POST/PUT), across all API paths. Any GET requests, or requests from different clients are not protected against concurrent writing.

func NewClient

func NewClient(url, usr, pwd string, mods ...func(*Client)) (Client, error)

NewClient creates a new FMC HTTP client. Pass modifiers in to modify the behavior of the client, e.g.

client, _ := NewClient("fmc1.cisco.com", "user", "password", RequestTimeout(120))

func NewClientCDFMC added in v0.2.0

func NewClientCDFMC(url, apiToken string, mods ...func(*Client)) (Client, error)

Create a new cdFMC HTTP client.

func (*Client) AuthToken

func (client *Client) AuthToken() string

AuthToken returns the current token

func (*Client) Authenticate

func (client *Client) Authenticate(currentAuthToken string) error

Authenticate assures the token is there and valid. It will try to login/refresh the token based on the current state and information from FMC on failures (no proactive reauthentications). currentAuthToken is the token used in the request. This helps to determine, if authToken needs refreshing or has already been refreshed by other thread. currentAuthToken can be an empty string.

func (*Client) Backoff

func (client *Client) Backoff(attempts int) bool

Backoff waits following an exponential backoff algorithm

func (*Client) Delete

func (client *Client) Delete(path string, mods ...func(*Req)) (Res, error)

Delete makes a DELETE request.

func (*Client) Do

func (client *Client) Do(req Req) (Res, error)

Do makes a request. Requests for Do are built ouside of the client, e.g.

req := client.NewReq("GET", "/api/fmc_config/v1/domain/{DOMAIN_UUID}/object/networks", nil)
res, _ := client.Do(req)

func (*Client) Get

func (client *Client) Get(path string, mods ...func(*Req)) (Res, error)

Get makes a GET requests and returns a GJSON result. It handles pagination and returns all items in a single response.

func (*Client) GetFMCVersion

func (client *Client) GetFMCVersion() error

Get FMC Version

func (*Client) NewReq

func (client *Client) NewReq(method, uri string, body io.Reader, mods ...func(*Req)) (Req, error)

NewReq creates a new Req request for this client. Use a "{DOMAIN_UUID}" placeholder in the URI to be replaced with the domain UUID.

func (*Client) Post

func (client *Client) Post(path, data string, mods ...func(*Req)) (Res, error)

Post makes a POST request and returns a GJSON result. Hint: Use the Body struct to easily create POST body data.

func (*Client) Put

func (client *Client) Put(path, data string, mods ...func(*Req)) (Res, error)

Put makes a PUT request and returns a GJSON result. Hint: Use the Body struct to easily create PUT body data.

type Req

type Req struct {
	// HttpReq is the *http.Request obejct.
	HttpReq *http.Request
	// LogPayload indicates whether logging of payloads should be enabled.
	LogPayload bool
	// DomainName is the FMC domain to be used for the request.
	DomainName string
	// ID for the request.
	RequestID string
}

Req wraps http.Request for API requests.

type Res

type Res = gjson.Result

Res is an API response returned by client requests. This is a GJSON result, which offers advanced and safe parsing capabilities. https://github.com/tidwall/gjson

Jump to

Keyboard shortcuts

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