Skip to content

Commit

Permalink
reduce linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
FoseFx committed Oct 20, 2024
1 parent b27e34f commit 4ceb546
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 41 deletions.
2 changes: 1 addition & 1 deletion cmd/spice/spice.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package main
import (
"context"
"fmt"
"hwauthz/spicedb/migrate"
"os"
"os/exec"
"path/filepath"
Expand All @@ -13,7 +14,6 @@ import (
"github.com/authzed/grpcutil"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"hwauthz/spicedb/migrate"
)

// CLI is filled by kong.Parse in main
Expand Down
28 changes: 17 additions & 11 deletions libs/hwauthz/spicedb/migrate/migrate.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
//nolint:forbidigo
package migrate

import (
"context"
"errors"
"fmt"
v1 "github.com/authzed/authzed-go/proto/authzed/api/v1"
"github.com/authzed/authzed-go/v1"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"io"
"os"
"path/filepath"
"strconv"
"strings"

v1 "github.com/authzed/authzed-go/proto/authzed/api/v1"
"github.com/authzed/authzed-go/v1"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

//
// This module is intended for CLIs, and thus panics, instead of returning errors
//

const migrationResourceType = "spice_schema_migrations/migration"
const migrationResourceId = "current"
const migrationRelation = "version"
const migrationSubjectType = "spice_schema_migrations/version"
const (
migrationResourceType = "spice_schema_migrations/migration"
migrationResourceId = "current"
migrationRelation = "version"
migrationSubjectType = "spice_schema_migrations/version"
)

// GetCurrentVersion queries existing relations to parse the currently deployed version of the schema
func GetCurrentVersion(ctx context.Context, client *authzed.Client) int {
Expand All @@ -42,11 +47,12 @@ func GetCurrentVersion(ctx context.Context, client *authzed.Client) int {
// now collect the first element
response, err := stream.Recv()
if err != nil {
if err == io.EOF {
if errors.Is(err, io.EOF) {
return 0
}
if statusErr, ok := status.FromError(err); ok {
if statusErr.Code() == codes.FailedPrecondition && statusErr.Message() == "object definition `spice_schema_migrations/migration` not found" {
if statusErr.Code() == codes.FailedPrecondition &&
statusErr.Message() == "object definition `spice_schema_migrations/migration` not found" {
return 0
}
}
Expand All @@ -63,7 +69,7 @@ func GetCurrentVersion(ctx context.Context, client *authzed.Client) int {

// if more relations exist, the version is not clear, user must fix it
_, err = stream.Recv()
if err != io.EOF {
if errors.Is(err, io.EOF) {
panic("more than one version relation exist")
}
return i
Expand Down
2 changes: 1 addition & 1 deletion libs/hwauthz/spicedb/spicedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (s *SpiceDBAuthZ) BulkCheck(ctx context.Context, checks []hwauthz.Permissio

keyPattern := "%s:%s-%s-%s:%s"

var items = make([]*v1.CheckBulkPermissionsRequestItem, 0, len(checks))
items := make([]*v1.CheckBulkPermissionsRequestItem, 0, len(checks))
for i, check := range checks {
sub := check.Subject
resc := check.Resource
Expand Down
14 changes: 7 additions & 7 deletions libs/hwauthz/spicedb/spicedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ package spicedb

import (
"context"
zlog "github.com/rs/zerolog/log"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"hwauthz"
"hwtesting"
"os"
"os/signal"
"strconv"
"telemetry"
"testing"
"time"

zlog "github.com/rs/zerolog/log"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"hwauthz"
)

func TestMain(m *testing.M) {
Expand Down Expand Up @@ -66,11 +67,11 @@ func TestBulkCheck(t *testing.T) {
// Create a few PermissionChecks

amount := 5
checks := make([]hwauthz.PermissionCheck, amount)
checks := make([]hwauthz.PermissionCheck, amount, amount+1)

tx := hwauthz.NewTx(client, nil, nil)

for i, _ := range checks {
for i := range checks {
sub := testObject{
typ: "user",
id: strconv.Itoa(i),
Expand Down Expand Up @@ -107,5 +108,4 @@ func TestBulkCheck(t *testing.T) {
assert.Equal(t, i%2 == 0, b, checks[i].DebugString())
}
}

}
2 changes: 1 addition & 1 deletion libs/hwauthz/test/true_authz.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (a *TrueAuthZ) Must(ctx context.Context, check hwauthz.PermissionCheck) err

func BulkCheck(_ context.Context, checks []hwauthz.PermissionCheck) ([]bool, error) {
bs := make([]bool, len(checks))
for i, _ := range bs {
for i := range bs {
bs[i] = true
}
return bs, nil
Expand Down
6 changes: 4 additions & 2 deletions libs/hwtesting/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ func (t InsecureBearerToken) RequireTransportSecurity() bool {
return false
}

const FakeTokenUser = "18159713-5d4e-4ad5-94ad-fbb6bb147984" //nolint:gosec
const FakeTokenOrganization = "3b25c6f5-4705-4074-9fc6-a50c28eba405" //nolint:gosec
const (
FakeTokenUser = "18159713-5d4e-4ad5-94ad-fbb6bb147984" //nolint:gosec
FakeTokenOrganization = "3b25c6f5-4705-4074-9fc6-a50c28eba405" //nolint:gosec
)

func GetFakeTokenCredentials(subOverride string) InsecureBearerToken {
// README's fake token
Expand Down
6 changes: 3 additions & 3 deletions libs/hwtesting/spicedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"

spicedb "github.com/Mariscal6/testcontainers-spicedb-go"
spicev1 "github.com/authzed/authzed-go/proto/authzed/api/v1"
Expand All @@ -13,9 +16,6 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
spiceMigrate "hwauthz/spicedb/migrate"
"os/exec"
"path/filepath"
"strings"
)

const (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"context"
"errors"
pb "gen/services/property_svc/v1"
"github.com/google/uuid"
"hwauthz"
"hwes"

"github.com/google/uuid"

"property-svc/internal/property/aggregate"
"property-svc/internal/property/models"
"property-svc/internal/property/perm"
Expand Down
9 changes: 6 additions & 3 deletions services/property-svc/internal/property/perm/permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package perm
import (
"common/auth"
"context"
"github.com/google/uuid"
"hwauthz"

"github.com/google/uuid"
)

// Types
Expand Down Expand Up @@ -48,5 +49,7 @@ const PropertyOrganization hwauthz.Relation = "organization"

const OrganizationCanUserCreateProperty hwauthz.Permission = "create_property"

const PropertyCanUserGet hwauthz.Permission = "get"
const PropertyCanUserUpdate hwauthz.Permission = "update"
const (
PropertyCanUserGet hwauthz.Permission = "get"
PropertyCanUserUpdate hwauthz.Permission = "update"
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ package spicedb_projection

import (
"context"
"errors"
"fmt"
"github.com/EventStore/EventStore-Client-Go/v4/esdb"
"github.com/google/uuid"
zlog "github.com/rs/zerolog/log"
"hwauthz"
"hwes"
"hwes/eventstoredb/projections/custom"
"hwutil"

"property-svc/internal/property/aggregate"

"github.com/EventStore/EventStore-Client-Go/v4/esdb"
"github.com/google/uuid"
zlog "github.com/rs/zerolog/log"

propertyEventsV1 "property-svc/internal/property/events/v1"
"property-svc/internal/property/perm"
)
Expand All @@ -21,10 +25,14 @@ type Projection struct {
}

func NewProjection(es *esdb.Client, serviceName string, authz hwauthz.AuthZ) *Projection {
subscriptionGroupName := fmt.Sprintf("%s-spicedb-projection", serviceName)
subscriptionGroupName := serviceName + "-spicedb-projection"
p := &Projection{
CustomProjection: custom.NewCustomProjection(es, subscriptionGroupName, &[]string{fmt.Sprintf("%s-", aggregate.PropertyAggregateType)}),
authz: authz,
CustomProjection: custom.NewCustomProjection(
es,
subscriptionGroupName,
&[]string{aggregate.PropertyAggregateType + "-"},
),
authz: authz,
}
p.initEventListeners()
return p
Expand All @@ -50,7 +58,7 @@ func (p *Projection) onPropertyCreated(ctx context.Context, evt hwes.Event) (err
}

if evt.OrganizationID == nil {
return fmt.Errorf("organization is missing from event"), hwutil.PtrTo(esdb.NackActionPark)
return errors.New("organization is missing from event"), hwutil.PtrTo(esdb.NackActionPark)
}
organizationID := *evt.OrganizationID

Expand All @@ -63,7 +71,6 @@ func (p *Projection) onPropertyCreated(ctx context.Context, evt hwes.Event) (err
_, err = p.authz.
Create(relationship).
Commit(ctx)

if err != nil {
return fmt.Errorf("could not create spice relationship %s: %w", relationship.DebugString(), err),
hwutil.PtrTo(esdb.NackActionRetry)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"common"
"context"
pb "gen/services/property_svc/v1"
"github.com/google/uuid"
"hwauthz"
"hwdb"
"hwutil"

"github.com/google/uuid"

"property-svc/internal/property/models"
"property-svc/internal/property/perm"
"property-svc/repos/property_repo"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"context"
"fmt"
pb "gen/services/property_svc/v1"
"github.com/google/uuid"
"hwauthz"
"hwdb"

"github.com/google/uuid"

"property-svc/internal/property/models"
"property-svc/internal/property/perm"
"property-svc/repos/property_repo"
Expand Down
6 changes: 5 additions & 1 deletion services/property-svc/stories/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ func TestMain(m *testing.M) {
ctx, cancel := context.WithCancel(context.Background())

zlog.Info().Msg("starting containers")
endpoints, teardownContainers := hwtesting.StartContainers(ctx, hwtesting.Postgres, hwtesting.Eventstore, hwtesting.Spice)
endpoints, teardownContainers := hwtesting.StartContainers(ctx,
hwtesting.Postgres,
hwtesting.Eventstore,
hwtesting.Spice,
)
postgresEndpoint := endpoints.Get(hwtesting.Postgres)
eventstoreEndpoint := endpoints.Get(hwtesting.Eventstore)
spiceDBEndpoint := endpoints.Get(hwtesting.Spice)
Expand Down

0 comments on commit 4ceb546

Please sign in to comment.