fix: type safe acl policy in api

This commit is contained in:
Johan Siebens
2022-06-14 14:41:09 +02:00
parent d5f71224f6
commit 58e1f38231
6 changed files with 297 additions and 118 deletions
+11 -26
View File
@@ -5,16 +5,14 @@ import (
"encoding/json"
"fmt"
"github.com/bufbuild/connect-go"
"github.com/jsiebens/ionscale/internal/domain"
api "github.com/jsiebens/ionscale/pkg/gen/ionscale/v1"
"github.com/muesli/coral"
"gopkg.in/yaml.v2"
"io/ioutil"
)
func getACLConfigCommand() *coral.Command {
command := &coral.Command{
Use: "get-acl",
Use: "get-acl-policy",
Short: "Get the ACL policy",
SilenceUsage: true,
}
@@ -46,29 +44,12 @@ func getACLConfigCommand() *coral.Command {
return err
}
var p domain.ACLPolicy
if err := json.Unmarshal(resp.Msg.Value, &p); err != nil {
marshal, err := json.MarshalIndent(resp.Msg.Policy, "", " ")
if err != nil {
return err
}
if asJson {
marshal, err := json.MarshalIndent(&p, "", " ")
if err != nil {
return err
}
fmt.Println()
fmt.Println(string(marshal))
} else {
marshal, err := yaml.Marshal(&p)
if err != nil {
return err
}
fmt.Println()
fmt.Println(string(marshal))
}
fmt.Println(string(marshal))
return nil
}
@@ -78,7 +59,7 @@ func getACLConfigCommand() *coral.Command {
func setACLConfigCommand() *coral.Command {
command := &coral.Command{
Use: "set-acl",
Use: "set-acl-policy",
Short: "Set ACL policy",
SilenceUsage: true,
}
@@ -100,6 +81,11 @@ func setACLConfigCommand() *coral.Command {
return err
}
var policy = &api.ACLPolicy{}
if err := json.Unmarshal(rawJson, policy); err != nil {
return err
}
client, err := target.createGRPCClient()
if err != nil {
return err
@@ -110,12 +96,11 @@ func setACLConfigCommand() *coral.Command {
return err
}
_, err = client.SetACLPolicy(context.Background(), connect.NewRequest(&api.SetACLPolicyRequest{TailnetId: tailnet.Id, Value: rawJson}))
_, err = client.SetACLPolicy(context.Background(), connect.NewRequest(&api.SetACLPolicyRequest{TailnetId: tailnet.Id, Policy: policy}))
if err != nil {
return err
}
fmt.Println()
fmt.Println("ACL policy updated successfully")
return nil
+5 -7
View File
@@ -2,11 +2,11 @@ package service
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/bufbuild/connect-go"
"github.com/jsiebens/ionscale/internal/domain"
"github.com/jsiebens/ionscale/internal/mapping"
api "github.com/jsiebens/ionscale/pkg/gen/ionscale/v1"
)
@@ -24,14 +24,12 @@ func (s *Service) GetACLPolicy(ctx context.Context, req *connect.Request[api.Get
return nil, connect.NewError(connect.CodeNotFound, fmt.Errorf("tailnet does not exist"))
}
policy := tailnet.ACLPolicy
marshal, err := json.Marshal(policy)
if err != nil {
var policy api.ACLPolicy
if err := mapping.CopyViaJson(&tailnet.ACLPolicy, &policy); err != nil {
return nil, err
}
return connect.NewResponse(&api.GetACLPolicyResponse{Value: marshal}), nil
return connect.NewResponse(&api.GetACLPolicyResponse{Policy: &policy}), nil
}
func (s *Service) SetACLPolicy(ctx context.Context, req *connect.Request[api.SetACLPolicyRequest]) (*connect.Response[api.SetACLPolicyResponse], error) {
@@ -49,7 +47,7 @@ func (s *Service) SetACLPolicy(ctx context.Context, req *connect.Request[api.Set
}
var policy domain.ACLPolicy
if err := json.Unmarshal(req.Msg.Value, &policy); err != nil {
if err := mapping.CopyViaJson(req.Msg.Policy, &policy); err != nil {
return nil, err
}