|
| 1 | +package usercontext |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/chainloop-dev/chainloop/app/controlplane/internal/usercontext/entities" |
| 8 | + "github.com/go-kratos/kratos/v2/transport" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +type suspensionMockTransport struct { |
| 14 | + operation string |
| 15 | +} |
| 16 | + |
| 17 | +func (tr *suspensionMockTransport) Kind() transport.Kind { return transport.KindGRPC } |
| 18 | +func (tr *suspensionMockTransport) Endpoint() string { return "" } |
| 19 | +func (tr *suspensionMockTransport) Operation() string { return tr.operation } |
| 20 | +func (tr *suspensionMockTransport) RequestHeader() transport.Header { return nil } |
| 21 | +func (tr *suspensionMockTransport) ReplyHeader() transport.Header { return nil } |
| 22 | + |
| 23 | +var passHandler = func(_ context.Context, _ interface{}) (interface{}, error) { return "ok", nil } |
| 24 | + |
| 25 | +func TestWithSuspensionMiddleware(t *testing.T) { |
| 26 | + suspendedOrg := &entities.Org{ID: "org-1", Name: "test", Suspended: true} |
| 27 | + activeOrg := &entities.Org{ID: "org-1", Name: "test", Suspended: false} |
| 28 | + |
| 29 | + tests := []struct { |
| 30 | + name string |
| 31 | + org *entities.Org |
| 32 | + operation string |
| 33 | + wantErr bool |
| 34 | + wantMsg string |
| 35 | + }{ |
| 36 | + { |
| 37 | + name: "no org context passes through", |
| 38 | + org: nil, |
| 39 | + wantErr: false, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "non-suspended org passes through", |
| 43 | + org: activeOrg, |
| 44 | + operation: "/controlplane.v1.WorkflowService/Create", |
| 45 | + wantErr: false, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "suspended org allows read operation", |
| 49 | + org: suspendedOrg, |
| 50 | + operation: "/controlplane.v1.ReferrerService/DiscoverPrivate", |
| 51 | + wantErr: false, |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "suspended org allows list operation", |
| 55 | + org: suspendedOrg, |
| 56 | + operation: "/controlplane.v1.WorkflowService/List", |
| 57 | + wantErr: false, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "suspended org allows read policy operation", |
| 61 | + org: suspendedOrg, |
| 62 | + operation: "/controlplane.v1.ContextService/Current", |
| 63 | + wantErr: false, |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "suspended org allows exempt empty-policy read", |
| 67 | + org: suspendedOrg, |
| 68 | + operation: "/controlplane.v1.CASCredentialsService/Get", |
| 69 | + wantErr: false, |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "suspended org allows exempt navigation operation", |
| 73 | + org: suspendedOrg, |
| 74 | + operation: "/controlplane.v1.UserService/ListMemberships", |
| 75 | + wantErr: false, |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "suspended org allows exempt group read", |
| 79 | + org: suspendedOrg, |
| 80 | + operation: "/controlplane.v1.GroupService/ListMembers", |
| 81 | + wantErr: false, |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "suspended org allows self-service org delete", |
| 85 | + org: suspendedOrg, |
| 86 | + operation: "/controlplane.v1.OrganizationService/Delete", |
| 87 | + wantErr: false, |
| 88 | + }, |
| 89 | + { |
| 90 | + name: "suspended org allows self-service leave org", |
| 91 | + org: suspendedOrg, |
| 92 | + operation: "/controlplane.v1.UserService/DeleteMembership", |
| 93 | + wantErr: false, |
| 94 | + }, |
| 95 | + { |
| 96 | + name: "suspended org allows self-service delete account", |
| 97 | + org: suspendedOrg, |
| 98 | + operation: "/controlplane.v1.AuthService/DeleteAccount", |
| 99 | + wantErr: false, |
| 100 | + }, |
| 101 | + { |
| 102 | + name: "suspended org blocks empty-policy write", |
| 103 | + org: suspendedOrg, |
| 104 | + operation: "/controlplane.v1.GroupService/AddMember", |
| 105 | + wantErr: true, |
| 106 | + wantMsg: "suspended", |
| 107 | + }, |
| 108 | + { |
| 109 | + name: "suspended org blocks another empty-policy write", |
| 110 | + org: suspendedOrg, |
| 111 | + operation: "/controlplane.v1.GroupService/RemoveMember", |
| 112 | + wantErr: true, |
| 113 | + wantMsg: "suspended", |
| 114 | + }, |
| 115 | + { |
| 116 | + name: "suspended org blocks empty-policy org create", |
| 117 | + org: suspendedOrg, |
| 118 | + operation: "/controlplane.v1.OrganizationService/Create", |
| 119 | + wantErr: true, |
| 120 | + wantMsg: "suspended", |
| 121 | + }, |
| 122 | + { |
| 123 | + name: "suspended org blocks unmapped write", |
| 124 | + org: suspendedOrg, |
| 125 | + operation: "/controlplane.v1.CASBackendService/Update", |
| 126 | + wantErr: true, |
| 127 | + wantMsg: "suspended", |
| 128 | + }, |
| 129 | + { |
| 130 | + name: "suspended org blocks another unmapped write", |
| 131 | + org: suspendedOrg, |
| 132 | + operation: "/controlplane.v1.OrgInvitationService/Revoke", |
| 133 | + wantErr: true, |
| 134 | + wantMsg: "suspended", |
| 135 | + }, |
| 136 | + { |
| 137 | + name: "suspended org blocks attestation write", |
| 138 | + org: suspendedOrg, |
| 139 | + operation: "/controlplane.v1.AttestationService/Init", |
| 140 | + wantErr: true, |
| 141 | + wantMsg: "suspended", |
| 142 | + }, |
| 143 | + { |
| 144 | + name: "suspended org blocks attestation state write", |
| 145 | + org: suspendedOrg, |
| 146 | + operation: "/controlplane.v1.AttestationStateService/Save", |
| 147 | + wantErr: true, |
| 148 | + wantMsg: "suspended", |
| 149 | + }, |
| 150 | + { |
| 151 | + name: "suspended org blocks signing write", |
| 152 | + org: suspendedOrg, |
| 153 | + operation: "/controlplane.v1.SigningService/GenerateSigningCert", |
| 154 | + wantErr: true, |
| 155 | + wantMsg: "suspended", |
| 156 | + }, |
| 157 | + { |
| 158 | + name: "suspended org blocks mapped write policy", |
| 159 | + org: suspendedOrg, |
| 160 | + operation: "/controlplane.v1.WorkflowService/Create", |
| 161 | + wantErr: true, |
| 162 | + wantMsg: "suspended", |
| 163 | + }, |
| 164 | + { |
| 165 | + name: "suspended org blocks mapped update policy", |
| 166 | + org: suspendedOrg, |
| 167 | + operation: "/controlplane.v1.CASBackendService/Revalidate", |
| 168 | + wantErr: true, |
| 169 | + wantMsg: "suspended", |
| 170 | + }, |
| 171 | + { |
| 172 | + name: "suspended org blocks OrganizationService/DeleteMembership despite Delete being exempt", |
| 173 | + org: suspendedOrg, |
| 174 | + operation: "/controlplane.v1.OrganizationService/DeleteMembership", |
| 175 | + wantErr: true, |
| 176 | + wantMsg: "suspended", |
| 177 | + }, |
| 178 | + { |
| 179 | + name: "suspended org blocks unknown future endpoint", |
| 180 | + org: suspendedOrg, |
| 181 | + operation: "/controlplane.v1.NewService/SomeWrite", |
| 182 | + wantErr: true, |
| 183 | + wantMsg: "suspended", |
| 184 | + }, |
| 185 | + { |
| 186 | + name: "suspended org with no transport context is blocked", |
| 187 | + org: suspendedOrg, |
| 188 | + wantErr: true, |
| 189 | + wantMsg: "suspended", |
| 190 | + }, |
| 191 | + } |
| 192 | + |
| 193 | + for _, tt := range tests { |
| 194 | + t.Run(tt.name, func(t *testing.T) { |
| 195 | + ctx := context.Background() |
| 196 | + |
| 197 | + if tt.org != nil { |
| 198 | + ctx = entities.WithCurrentOrg(ctx, tt.org) |
| 199 | + } |
| 200 | + |
| 201 | + if tt.operation != "" { |
| 202 | + ctx = transport.NewServerContext(ctx, &suspensionMockTransport{operation: tt.operation}) |
| 203 | + } |
| 204 | + |
| 205 | + m := WithSuspensionMiddleware() |
| 206 | + result, err := m(passHandler)(ctx, nil) |
| 207 | + |
| 208 | + if tt.wantErr { |
| 209 | + require.Error(t, err) |
| 210 | + assert.Contains(t, err.Error(), tt.wantMsg) |
| 211 | + assert.Nil(t, result) |
| 212 | + } else { |
| 213 | + require.NoError(t, err) |
| 214 | + assert.Equal(t, "ok", result) |
| 215 | + } |
| 216 | + }) |
| 217 | + } |
| 218 | +} |
0 commit comments