Skip to content

Commit

Permalink
Merge branch 'main' of github.com:casbin/casbin-mesh
Browse files Browse the repository at this point in the history
  • Loading branch information
WenyXu committed Jun 26, 2022
2 parents 883fed3 + 4277623 commit 0643b6e
Show file tree
Hide file tree
Showing 12 changed files with 974 additions and 517 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ COPY ./go.mod ./
COPY ./go.sum ./
RUN go mod download
COPY . .
RUN export GO111MODULE=on && CGO_ENABLED=0 GOOS=linux go build -ldflags "-s -w" -o build/casmesh cmd/app/main.go
RUN export GO111MODULE=on && CGO_ENABLED=0 GOOS=linux go build -ldflags "-s -w" -o build/casmesh cmd/app/*.go


FROM alpine:latest
Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/auth.go → client/v2/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

package main
package client

type AuthType = string

Expand Down
65 changes: 31 additions & 34 deletions cmd/cli/client.go → client/v2/client.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
// Copyright 2022 The casbin-neo Authors. All Rights Reserved.
//
// http://www.apache.org/licenses/LICENSE-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main
package client

import (
"context"
Expand All @@ -29,23 +26,23 @@ import (
"time"
)

type client struct {
type Client struct {
grpcClient command.CasbinMeshClient
}

var (
MarshalFailed = errors.New("marshal failed")
)

func (c client) ShowStats(ctx context.Context) ([]byte, error) {
func (c Client) ShowStats(ctx context.Context) ([]byte, error) {
resp, err := c.grpcClient.ShowStats(ctx, &command.StatsRequest{})
if err != nil {
return nil, err
}
return resp.Payload, nil
}

func (c client) AddPolicies(ctx context.Context, namespace, sec, ptype string, rules [][]string) ([][]string, error) {
func (c Client) AddPolicies(ctx context.Context, namespace, sec, ptype string, rules [][]string) ([][]string, error) {
payload := command.AddPoliciesPayload{
Sec: sec,
PType: ptype,
Expand All @@ -70,7 +67,7 @@ func (c client) AddPolicies(ctx context.Context, namespace, sec, ptype string, r
return command.ToStringArray(resp.EffectedRules), nil
}

func (c client) RemovePolicies(ctx context.Context, namespace, sec, ptype string, rules [][]string) ([][]string, error) {
func (c Client) RemovePolicies(ctx context.Context, namespace, sec, ptype string, rules [][]string) ([][]string, error) {
payload := command.RemovePoliciesPayload{
Sec: sec,
PType: ptype,
Expand All @@ -95,7 +92,7 @@ func (c client) RemovePolicies(ctx context.Context, namespace, sec, ptype string
return command.ToStringArray(resp.EffectedRules), nil
}

func (c client) UpdatePolicies(ctx context.Context, namespace, sec, ptype string, old, new [][]string) (bool, error) {
func (c Client) UpdatePolicies(ctx context.Context, namespace, sec, ptype string, old, new [][]string) (bool, error) {
log.Printf("sec:%s,ptype:%s,or%v,nr:%v", sec, ptype, old, new)
payload := command.UpdatePoliciesPayload{
Sec: sec,
Expand All @@ -122,7 +119,7 @@ func (c client) UpdatePolicies(ctx context.Context, namespace, sec, ptype string
return resp.Effected, nil
}

func (c client) ListNamespaces(ctx context.Context) ([]string, error) {
func (c Client) ListNamespaces(ctx context.Context) ([]string, error) {
resp, err := c.grpcClient.ListNamespaces(ctx, &command.ListNamespacesRequest{})
if err != nil {
return nil, err
Expand All @@ -133,15 +130,15 @@ func (c client) ListNamespaces(ctx context.Context) ([]string, error) {
return resp.Namespace, nil
}

func (c client) ListPolicies(ctx context.Context, namespace string) ([][]string, error) {
func (c Client) ListPolicies(ctx context.Context, namespace string) ([][]string, error) {
resp, err := c.grpcClient.ListPolicies(ctx, &command.ListPoliciesRequest{Namespace: namespace})
if err != nil {
return nil, err
}
return command.ToStringArray(resp.Policies), nil
}

func (c client) Enforce(ctx context.Context, namespace string, level command.EnforcePayload_Level, freshness int64, params ...interface{}) (bool, error) {
func (c Client) Enforce(ctx context.Context, namespace string, level command.EnforcePayload_Level, freshness int64, params ...interface{}) (bool, error) {
var B [][]byte
for _, p := range params {
b, err := json.Marshal(p)
Expand Down Expand Up @@ -170,7 +167,7 @@ func (c client) Enforce(ctx context.Context, namespace string, level command.Enf
return result.Ok, nil
}

func (c client) PrintModel(ctx context.Context, namespace string) (string, error) {
func (c Client) PrintModel(ctx context.Context, namespace string) (string, error) {
resp, err := c.grpcClient.PrintModel(ctx, &command.PrintModelRequest{Namespace: namespace})
if err != nil {
return "", err
Expand All @@ -181,31 +178,31 @@ func (c client) PrintModel(ctx context.Context, namespace string) (string, error
return resp.Model, nil
}

type options struct {
target string
authType AuthType
username string
password string
type Options struct {
Target string
AuthType AuthType
Username string
Password string
}

func NewClient(op options) *client {
func NewClient(op Options) *Client {
var opts []grpc.DialOption
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
opts = append(opts, grpc.WithInsecure())
opts = append(opts, grpc.WithBlock())

switch op.authType {
switch op.AuthType {
case Basic:
opts = append(opts, grpc.WithUnaryInterceptor(grpc_middleware.ChainUnaryClient(BasicAuthor(op.username, op.password))))
opts = append(opts, grpc.WithUnaryInterceptor(grpc_middleware.ChainUnaryClient(BasicAuthor(op.Username, op.Password))))
}

conn, err := grpc.DialContext(ctx, op.target, opts...)
conn, err := grpc.DialContext(ctx, op.Target, opts...)
if err != nil {
log.Fatalf("fail to dial: %v", err)
}
log.Println("login success!")
//defer conn.Close()
c := command.NewCasbinMeshClient(conn)
return &client{grpcClient: c}
return &Client{grpcClient: c}
}
4 changes: 2 additions & 2 deletions cmd/cli/client_test.go → client/v2/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

package main
package client

import (
"context"
Expand All @@ -26,7 +26,7 @@ import (
)

var (
c *client
c *Client
)

func TestMain(m *testing.M) {
Expand Down
18 changes: 18 additions & 0 deletions client/v2/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module github.com/casbin/casbin-mesh/client/v2

go 1.18

require (
github.com/casbin/casbin-mesh v0.0.0-20220510133536-c1b32d87368a
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
google.golang.org/grpc v1.47.0
)

require (
github.com/golang/protobuf v1.5.2 // indirect
golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb // indirect
golang.org/x/sys v0.0.0-20210816183151-1e6c022a8912 // indirect
golang.org/x/text v0.3.3 // indirect
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect
google.golang.org/protobuf v1.27.1 // indirect
)
Loading

0 comments on commit 0643b6e

Please sign in to comment.