Skip to content

Commit

Permalink
Merge pull request #10 from bamboo-firewall/feature/bbfwcli
Browse files Browse the repository at this point in the history
bamboo firewall cli
  • Loading branch information
bienkma authored Oct 22, 2024
2 parents f9c64c7 + 26ddf3f commit 02a0f7a
Show file tree
Hide file tree
Showing 46 changed files with 1,581 additions and 39 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*.dll
*.so
*.dylib
_output/

# Test binary, built with `go test -c`
*.test
Expand Down
24 changes: 24 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
SERVER_DIR = ./cmd/server
SERVER_BIN_NAME = bamboo-apiserver
CLI_DIR = ./cmd/bamboofwcli
CLI_BIN_NAME = bbfw

.PHONY: all-platform
all-platform:
build/build.sh $(SERVER_DIR) $(SERVER_BIN_NAME) all
build/build.sh $(CLI_DIR) $(CLI_BIN_NAME) all

.PHONY: all
all: build-server build-bbfw

.PHONY: build-server
build-server:
build/build.sh $(SERVER_DIR) $(SERVER_BIN_NAME)

.PHONY: build-bbfw
build-bbfw:
build/build.sh $(CLI_DIR) $(CLI_BIN_NAME)

.PHONY: clean
clean:
build/clean.sh
14 changes: 9 additions & 5 deletions api/v1/dto/gnp.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package dto
import "time"

type GlobalNetworkPolicy struct {
ID string `json:"id"`
UUID string `json:"uuid"`
Version uint `json:"version"`
ID string `json:"id" yaml:"id"`
UUID string `json:"uuid" yaml:"uuid"`
Version uint `json:"version" yaml:"version"`
Metadata GNPMetadata `json:"metadata,omitempty" yaml:"metadata,omitempty"`
Spec GNPSpec `json:"spec" yaml:"spec"`
Description string `json:"description,omitempty" yaml:"description"`
Expand All @@ -29,7 +29,7 @@ type GNPSpecRule struct {
Action string `json:"action" yaml:"action"`
Protocol string `json:"protocol,omitempty" yaml:"protocol"`
NotProtocol string `json:"notProtocol,omitempty" yaml:"notProtocol"`
IPVersion int `json:"ipVersion"`
IPVersion int `json:"ipVersion" yaml:"ipVersion"`
Source *GNPSpecRuleEntity `json:"source,omitempty" yaml:"source"`
Destination *GNPSpecRuleEntity `json:"destination,omitempty" yaml:"destination"`
}
Expand Down Expand Up @@ -64,7 +64,7 @@ type GNPSpecRuleInput struct {
Action string `json:"action" yaml:"action" validate:"required,action"`
Protocol string `json:"protocol" yaml:"protocol" validate:"omitempty,protocol"`
NotProtocol string `json:"notProtocol" yaml:"notProtocol" validate:"omitempty,protocol"`
IPVersion int `json:"ipVersion" validate:"required,ip_version"`
IPVersion int `json:"ipVersion" yaml:"ipVersion" validate:"required,ip_version"`
Source *GNPSpecRuleEntityInput `json:"source" yaml:"source" validate:"omitempty"`
Destination *GNPSpecRuleEntityInput `json:"destination" yaml:"destination" validate:"omitempty"`
}
Expand All @@ -77,6 +77,10 @@ type GNPSpecRuleEntityInput struct {
NotPorts []interface{} `json:"notPorts" yaml:"notPorts" validate:"omitempty,min=1,unique,dive"`
}

type GetGNPInput struct {
Name string `uri:"name" validate:"required"`
}

type DeleteGlobalNetworkPolicyInput struct {
Metadata GNPMetadataInput `json:"metadata" yaml:"metadata" validate:"required"`
}
40 changes: 22 additions & 18 deletions api/v1/dto/gns.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,44 @@ package dto
import "time"

type GlobalNetworkSet struct {
ID string `json:"id"`
UUID string `json:"uuid"`
Version uint `json:"version"`
Metadata GNSMetadata `json:"metadata"`
Spec GNSSpec `json:"spec"`
Description string `json:"description"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
ID string `json:"id" yaml:"id"`
UUID string `json:"uuid" yaml:"uuid"`
Version uint `json:"version" yaml:"version"`
Metadata GNSMetadata `json:"metadata" yaml:"metadata"`
Spec GNSSpec `json:"spec" yaml:"spec"`
Description string `json:"description" yaml:"description"`
CreatedAt time.Time `json:"createdAt" yaml:"createdAt"`
UpdatedAt time.Time `json:"updatedAt" yaml:"updatedAt"`
}

type GNSMetadata struct {
Name string `json:"name"`
Labels map[string]string `json:"labels"`
Name string `json:"name" yaml:"name"`
Labels map[string]string `json:"labels" yaml:"labels"`
}

type GNSSpec struct {
Nets []string `json:"nets"`
Nets []string `json:"nets" yaml:"nets"`
}

type CreateGlobalNetworkSetInput struct {
Metadata GNSMetadataInput `json:"metadata" validate:"required"`
Spec GNSSpecInput `json:"spec"`
Description string `json:"description"`
Metadata GNSMetadataInput `json:"metadata" yaml:"metadata" validate:"required"`
Spec GNSSpecInput `json:"spec" yaml:"spec"`
Description string `json:"description" yaml:"description"`
}

type GNSMetadataInput struct {
Name string `json:"name" validate:"required,name"`
Labels map[string]string `json:"labels"`
Name string `json:"name" yaml:"name" validate:"required,name"`
Labels map[string]string `json:"labels" yaml:"labels"`
}

type GNSSpecInput struct {
Nets []string `json:"nets" validate:"min=1,unique"`
Nets []string `json:"nets" yaml:"nets" validate:"min=1,unique"`
}

type GetGNSInput struct {
Name string `uri:"name" validate:"required"`
}

type DeleteGlobalNetworkSetInput struct {
Metadata GNSMetadataInput `json:"metadata" validate:"required"`
Metadata GNSMetadataInput `json:"metadata" yaml:"metadata" validate:"required"`
}
12 changes: 8 additions & 4 deletions api/v1/dto/hep.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
)

type HostEndpoint struct {
ID string `json:"id"`
UUID string `json:"uuid"`
Version uint `json:"version"`
ID string `json:"id" yaml:"id"`
UUID string `json:"uuid" yaml:"uuid"`
Version uint `json:"version" yaml:"version"`
Metadata HostEndpointMetadata `json:"metadata" yaml:"metadata"`
Spec HostEndpointSpec `json:"spec" yaml:"spec"`
Description string `json:"description" yaml:"description"`
Expand All @@ -27,7 +27,7 @@ type HostEndpointSpec struct {

type CreateHostEndpointInput struct {
Metadata HostEndpointMetadataInput `json:"metadata" yaml:"metadata" validate:"required"`
Spec HostEndpointSpecInput `json:"spec" yaml:"spec"`
Spec HostEndpointSpecInput `json:"spec" yaml:"spec" validate:"required"`
Description string `json:"description" yaml:"description"`
}

Expand All @@ -41,6 +41,10 @@ type HostEndpointSpecInput struct {
IPs []string `json:"ips" yaml:"ips" validate:"min=1,unique,dive,ip"`
}

type GetHostEndpointInput struct {
Name string `uri:"name" validate:"required"`
}

type DeleteHostEndpointInput struct {
Metadata HostEndpointMetadataInput `json:"metadata" yaml:"metadata" validate:"required"`
}
Expand Down
16 changes: 16 additions & 0 deletions api/v1/handler/gnp.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

type gnpService interface {
Create(ctx context.Context, input *model.CreateGlobalNetworkPolicyInput) (*entity.GlobalNetworkPolicy, *ierror.Error)
Get(ctx context.Context, name string) (*entity.GlobalNetworkPolicy, *ierror.Error)
Delete(ctx context.Context, name string) *ierror.Error
}

Expand Down Expand Up @@ -44,6 +45,21 @@ func (h *gnp) Create(c *gin.Context) {
httpbase.ReturnSuccessResponse(c, http.StatusOK, mapper.ToGlobalNetworkPolicyDTO(gnsEntity))
}

func (h *gnp) Get(c *gin.Context) {
in := new(dto.GetGNPInput)
if ierr := httpbase.BindInput(c, in); ierr != nil {
httpbase.ReturnErrorResponse(c, ierr)
return
}

gnpEntity, ierr := h.service.Get(c.Request.Context(), in.Name)
if ierr != nil {
httpbase.ReturnErrorResponse(c, ierr)
return
}
httpbase.ReturnSuccessResponse(c, http.StatusOK, mapper.ToGlobalNetworkPolicyDTO(gnpEntity))
}

func (h *gnp) Delete(c *gin.Context) {
in := new(dto.DeleteGlobalNetworkSetInput)
if ierr := httpbase.BindInput(c, in); ierr != nil {
Expand Down
16 changes: 16 additions & 0 deletions api/v1/handler/gns.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

type gnsService interface {
Create(ctx context.Context, input *model.CreateGlobalNetworkSetInput) (*entity.GlobalNetworkSet, *ierror.Error)
Get(ctx context.Context, name string) (*entity.GlobalNetworkSet, *ierror.Error)
Delete(ctx context.Context, name string) *ierror.Error
}

Expand Down Expand Up @@ -44,6 +45,21 @@ func (h *gns) Create(c *gin.Context) {
httpbase.ReturnSuccessResponse(c, http.StatusOK, mapper.ToGlobalNetworkSetDTO(gnsEntity))
}

func (h *gns) Get(c *gin.Context) {
in := new(dto.GetGNSInput)
if ierr := httpbase.BindInput(c, in); ierr != nil {
httpbase.ReturnErrorResponse(c, ierr)
return
}

gnsEntity, ierr := h.service.Get(c.Request.Context(), in.Name)
if ierr != nil {
httpbase.ReturnErrorResponse(c, ierr)
return
}
httpbase.ReturnSuccessResponse(c, http.StatusOK, mapper.ToGlobalNetworkSetDTO(gnsEntity))
}

func (h *gns) Delete(c *gin.Context) {
in := new(dto.DeleteGlobalNetworkSetInput)
if ierr := httpbase.BindInput(c, in); ierr != nil {
Expand Down
16 changes: 16 additions & 0 deletions api/v1/handler/hep.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

type hepService interface {
Create(ctx context.Context, input *model.CreateHostEndpointInput) (*entity.HostEndpoint, *ierror.Error)
Get(ctx context.Context, name string) (*entity.HostEndpoint, *ierror.Error)
Delete(ctx context.Context, name string) *ierror.Error
FetchPolicies(ctx context.Context, input *model.FetchHostEndpointPolicyInput) (*model.HostEndPointPolicy, *ierror.Error)
}
Expand Down Expand Up @@ -45,6 +46,21 @@ func (h *hep) Create(c *gin.Context) {
httpbase.ReturnSuccessResponse(c, http.StatusOK, mapper.ToHostEndpointDTO(hepEntity))
}

func (h *hep) Get(c *gin.Context) {
in := new(dto.GetHostEndpointInput)
if ierr := httpbase.BindInput(c, in); ierr != nil {
httpbase.ReturnErrorResponse(c, ierr)
return
}

hepEntity, ierr := h.service.Get(c.Request.Context(), in.Name)
if ierr != nil {
httpbase.ReturnErrorResponse(c, ierr)
return
}
httpbase.ReturnSuccessResponse(c, http.StatusOK, mapper.ToHostEndpointDTO(hepEntity))
}

func (h *hep) Delete(c *gin.Context) {
in := new(dto.DeleteHostEndpointInput)
if ierr := httpbase.BindInput(c, in); ierr != nil {
Expand Down
4 changes: 2 additions & 2 deletions api/v1/mapper/gnp.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func ToGlobalNetworkPolicyDTO(gnp *entity.GlobalNetworkPolicy) *dto.GlobalNetwor
Egress: specEgress,
},
Description: gnp.Description,
CreatedAt: gnp.CreatedAt,
UpdatedAt: gnp.UpdatedAt,
CreatedAt: gnp.CreatedAt.Local(),
UpdatedAt: gnp.UpdatedAt.Local(),
}
}

Expand Down
4 changes: 2 additions & 2 deletions api/v1/mapper/gns.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func ToGlobalNetworkSetDTO(gns *entity.GlobalNetworkSet) *dto.GlobalNetworkSet {
Nets: gns.Spec.Nets,
},
Description: gns.Description,
CreatedAt: gns.CreatedAt,
UpdatedAt: gns.UpdatedAt,
CreatedAt: gns.CreatedAt.Local(),
UpdatedAt: gns.UpdatedAt.Local(),
}
}

Expand Down
4 changes: 2 additions & 2 deletions api/v1/mapper/hep.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func ToHostEndpointDTO(hep *entity.HostEndpoint) *dto.HostEndpoint {
IPs: hep.Spec.IPs,
},
Description: hep.Description,
CreatedAt: hep.CreatedAt,
UpdatedAt: hep.UpdatedAt,
CreatedAt: hep.CreatedAt.Local(),
UpdatedAt: hep.UpdatedAt.Local(),
}
}

Expand Down
8 changes: 8 additions & 0 deletions build/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

ROOT_PATH="$(cd "$(dirname "$0")/.." && pwd -P)"
BUILD_PATH="${ROOT_PATH}/build"

source "${BUILD_PATH}/init.sh"

golang::build_binaries "$@"
12 changes: 12 additions & 0 deletions build/clean.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash

ROOT_PATH="$(cd "$(dirname "$0")/.." && pwd -P)"
BUILD_PATH="${ROOT_PATH}/build"

source "${BUILD_PATH}/init.sh"

cleanup() {
rm -r ${BUILD_OUTPUT_PATH}
}

cleanup
41 changes: 41 additions & 0 deletions build/golang.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env bash

readonly SUPPORTED_PLATFORMS=(
linux/amd64
linux/arm64
darwin/amd64
darwin/arm64
windows/amd64
windows/arm64
)

golang::build_binaries() {
local -a platforms
if [[ "$3" == "all" ]]; then
platforms=("${SUPPORTED_PLATFORMS[@]}")
else
local host_platform
host_platform=$(golang::host_platform)
platforms+=("${host_platform}")
fi

for platform in "${platforms[@]}"; do
golang::build_binary_for_platform ${platform} $1 $2
done
}

golang::build_binary_for_platform() {
local platform="$1"
local build_dir="$2"
local bin_name="$3"

GOOS=${platform%%/*}
GOARCH=${platform##*/}
output="${BUILD_CMD_PATH}/${GOOS}/${GOARCH}/${bin_name}"

CGO_ENABLED=${CGO_ENABLED} GOOS=${GOOS} GOARCH=${GOARCH} ${GO_BUILD} -ldflags="${LDFLAGS}" -o ${output} ${build_dir}
}

golang::host_platform() {
echo "$(go env GOHOSTOS)/$(go env GOHOSTARCH)"
}
23 changes: 23 additions & 0 deletions build/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash

BUILD_OUTPUT_PATH="${ROOT_PATH}/_output"
BUILD_CMD_PATH="${BUILD_OUTPUT_PATH}/bin"

PACKAGE_NAME="github.com/bamboo-firewall/be"

VERSION="$(git describe --abbrev=0 --tags)"
BRANCH="$(git rev-parse --abbrev-ref HEAD)"
BUILD_TIME="$(date +%Y-%m-%dT%H:%M:%S%z)"

ORGANIZATION="ATAOCloud"

LDFLAGS="-s -w -X ${PACKAGE_NAME}/buildinfo.Version=${VERSION} \
-X ${PACKAGE_NAME}/buildinfo.GitBranch=${BRANCH} \
-X ${PACKAGE_NAME}/buildinfo.BuildDate=${BUILD_TIME} \
-X ${PACKAGE_NAME}/buildinfo.Organization=${ORGANIZATION}"

CGO_ENABLED=0

GO_BUILD="go build -buildvcs=false -a -installsuffix cgo"

source "${BUILD_PATH}/golang.sh"
8 changes: 8 additions & 0 deletions buildinfo/build_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package buildinfo

var (
Version string
GitBranch string
BuildDate string
Organization string
)
Loading

0 comments on commit 02a0f7a

Please sign in to comment.