Skip to content

wallarm/wallarm-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

88 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

wallarm-go

build PkgGoDev codecov Go Report Card License: MIT

Table of Contents

Note: This library is in active development and highly suggested to use carefully.

A Go library for interacting with Wallarm API. This library allows you to:

  • Manage applications
  • Manage nodes
  • Manage integrations
  • Manage triggers
  • Manage users
  • Manage the denylist
  • Switch the WAF/Scanner/Active Threat Verification modes
  • Inquire found vulnerabilities

Install

You need a working Go environment

go get github.com/wallarm/wallarm-go

Getting Started

The sample code could be similar

package main

import (
	"log"
	"net/http"
	"os"

	wallarm "github.com/wallarm/wallarm-go"
)

func main() {

	wapiHost, exist := os.LookupEnv("WALLARM_API_HOST")
	if !exist {
		wapiHost = "https://api.wallarm.com"
	}
	wapiToken, exist := os.LookupEnv("WALLARM_API_TOKEN")
	if !exist {
		log.Fatal("ENV variable WALLARM_API_TOKEN is not present")
	}

	authHeaders := make(http.Header)
	authHeaders.Add("X-WallarmAPI-Token", wapiToken)

	// Construct a new API object
	api, err := wallarm.New(wallarm.UsingBaseURL(wapiHost), wallarm.Headers(authHeaders))
	if err != nil {
		log.Print(err)
	}

	// Fetch user details
	u, err := api.UserDetails()
	if err != nil {
		log.Print(err)
	}
	// Print user specific data
	log.Println(u.Body)

	// Change global Wallarm mode to monitoring
	clientID := 1
	modeParams := wallarm.WallarmMode{Mode: "monitoring"}

	mode, err := api.WallarmModeUpdate(&modeParams, clientID)
	if err != nil {
		log.Print(err)
	}
	// Print Wallarm mode
	log.Println(mode)

	// Create a trigger when the number of attacks more than 1000 in 10 minutes
	filter := wallarm.TriggerFilters{
		ID:       "ip_address",
		Operator: "eq",
		Values:   []interface{}{"2.2.2.2"},
	}

	var filters []wallarm.TriggerFilters
	filters = append(filters, filter)

	action := wallarm.TriggerActions{
		ID: "send_notification",
		Params: wallarm.TriggerActionParams{
			IntegrationIds: []int{5},
		},
	}

	var actions []wallarm.TriggerActions
	actions = append(actions, action)

	triggerBody := wallarm.TriggerCreate{
		Trigger: &wallarm.TriggerParam{
			Name:       "New Terraform Trigger Telegram",
			Comment:    "This is a description set by Terraform",
			TemplateID: "attacks_exceeded",
			Enabled:    true,
			Filters:    &filters,
			Actions:    &actions,
		},
	}

	triggerResp, err := api.TriggerCreate(&triggerBody, 1)
	if err != nil {
		log.Print(err)
	}
	// Print trigger metadata
	log.Println(triggerResp)
}

License

MIT licensed