|
| 1 | +package token |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "slices" |
| 6 | + |
| 7 | + "github.com/ilijamt/vault-plugin-secrets-gitlab/internal/errs" |
| 8 | +) |
| 9 | + |
| 10 | +type Scope string |
| 11 | + |
| 12 | +const ( |
| 13 | + // ScopeApi grants complete read/write access to the API, including all groups and projects, the container registry, the dependency proxy, and the package registry. Also grants complete read/write access to the registry and repository using Git over HTTP |
| 14 | + ScopeApi = Scope("api") |
| 15 | + // ScopeReadApi grants read access to the scoped group and related project API, including the Package Registry |
| 16 | + ScopeReadApi = Scope("read_api") |
| 17 | + // ScopeReadRegistry grants read access (pull) to the Container Registry images if any project within expected group is private and authorization is required. |
| 18 | + ScopeReadRegistry = Scope("read_registry") |
| 19 | + // ScopeWriteRegistry grants write access (push) to the Container Registry. |
| 20 | + ScopeWriteRegistry = Scope("write_registry") |
| 21 | + // ScopeReadRepository grants read access (pull) to the Container Registry images if any project within expected group is private and authorization is required |
| 22 | + ScopeReadRepository = Scope("read_repository") |
| 23 | + // ScopeWriteRepository grants read and write access (pull and push) to all repositories within expected group |
| 24 | + ScopeWriteRepository = Scope("write_repository") |
| 25 | + |
| 26 | + // ScopeReadPackageRegistry Allows read-only access to the package registry. |
| 27 | + ScopeReadPackageRegistry = Scope("read_package_registry") |
| 28 | + // ScopeWritePackageRegistry Allows read and write access to the package registry. |
| 29 | + ScopeWritePackageRegistry = Scope("write_package_registry") |
| 30 | + |
| 31 | + // ScopeCreateRunner grants permission to create runners in expected group |
| 32 | + ScopeCreateRunner = Scope("create_runner") |
| 33 | + // ScopeManageRunner grants permission to manage runners in expected group |
| 34 | + ScopeManageRunner = Scope("manage_runner") |
| 35 | + |
| 36 | + // ScopeReadUser grants read-only access to the authenticated user’s profile through the /user API endpoint, which includes username, public email, and full name. Also grants access to read-only API endpoints under /users. |
| 37 | + ScopeReadUser = Scope("read_user") |
| 38 | + // ScopeSudo grants permission to perform API actions as any user in the system, when authenticated as an administrator. |
| 39 | + ScopeSudo = Scope("sudo") |
| 40 | + // ScopeAdminMode grants permission to perform API actions as an administrator, when Admin Mode is enabled. |
| 41 | + ScopeAdminMode = Scope("admin_mode") |
| 42 | + |
| 43 | + // ScopeAiFeatures grants permission to perform API actions for GitLab Duo. This scope is designed to work with the GitLab Duo Plugin for JetBrains. For all other extensions, see scope requirements. |
| 44 | + ScopeAiFeatures = Scope("ai_features") |
| 45 | + // ScopeK8SProxy grants permission to perform Kubernetes API calls using the agent for Kubernetes. |
| 46 | + ScopeK8SProxy = Scope("k8s_proxy") |
| 47 | + // ScopeReadServicePing grant access to download Service Ping payload through the API when authenticated as an admin use. |
| 48 | + ScopeReadServicePing = Scope("read_service_ping") |
| 49 | + |
| 50 | + // ScopeSelfRotate grants permission to rotate this token using the personal access token API. Does not allow rotation of other tokens. |
| 51 | + ScopeSelfRotate = Scope("self_rotate") |
| 52 | + // ScopeReadVirtualRegistry if a project is private and authorization is required, grants read-only (pull) access to container images through the dependency proxy. Available only when the dependency proxy is enabled. |
| 53 | + ScopeReadVirtualRegistry = Scope("read_virtual_registry") |
| 54 | + // ScopeWriteVirtualRegistry if a project is private and authorization is required, grants read (pull), write (push), and delete access to container images through the dependency proxy. Available only when the dependency proxy is enabled. |
| 55 | + ScopeWriteVirtualRegistry = Scope("write_virtual_registry") |
| 56 | + |
| 57 | + ScopeUnknown = Scope("") |
| 58 | +) |
| 59 | + |
| 60 | +var ( |
| 61 | + |
| 62 | + // ValidPersonalTokenScopes defines the actions you can perform when you authenticate with a project access token. |
| 63 | + ValidPersonalTokenScopes = []string{ |
| 64 | + ScopeApi.String(), |
| 65 | + ScopeReadUser.String(), |
| 66 | + ScopeReadApi.String(), |
| 67 | + ScopeReadRepository.String(), |
| 68 | + ScopeWriteRepository.String(), |
| 69 | + ScopeReadRegistry.String(), |
| 70 | + ScopeWriteRegistry.String(), |
| 71 | + ScopeReadVirtualRegistry.String(), |
| 72 | + ScopeWriteVirtualRegistry.String(), |
| 73 | + ScopeSudo.String(), |
| 74 | + ScopeAdminMode.String(), |
| 75 | + ScopeCreateRunner.String(), |
| 76 | + ScopeManageRunner.String(), |
| 77 | + ScopeAiFeatures.String(), |
| 78 | + ScopeK8SProxy.String(), |
| 79 | + ScopeSelfRotate.String(), |
| 80 | + ScopeReadServicePing.String(), |
| 81 | + } |
| 82 | + |
| 83 | + ValidProjectTokenScopes = []string{ |
| 84 | + ScopeApi.String(), |
| 85 | + ScopeReadApi.String(), |
| 86 | + ScopeReadRegistry.String(), |
| 87 | + ScopeWriteRegistry.String(), |
| 88 | + ScopeReadRepository.String(), |
| 89 | + ScopeWriteRepository.String(), |
| 90 | + ScopeCreateRunner.String(), |
| 91 | + ScopeManageRunner.String(), |
| 92 | + ScopeAiFeatures.String(), |
| 93 | + ScopeK8SProxy.String(), |
| 94 | + ScopeSelfRotate.String(), |
| 95 | + } |
| 96 | + |
| 97 | + ValidGroupTokenScopes = []string{ |
| 98 | + ScopeApi.String(), |
| 99 | + ScopeReadApi.String(), |
| 100 | + ScopeReadRegistry.String(), |
| 101 | + ScopeWriteRegistry.String(), |
| 102 | + ScopeReadVirtualRegistry.String(), |
| 103 | + ScopeWriteVirtualRegistry.String(), |
| 104 | + ScopeReadRepository.String(), |
| 105 | + ScopeWriteRepository.String(), |
| 106 | + ScopeCreateRunner.String(), |
| 107 | + ScopeManageRunner.String(), |
| 108 | + ScopeAiFeatures.String(), |
| 109 | + ScopeK8SProxy.String(), |
| 110 | + ScopeSelfRotate.String(), |
| 111 | + } |
| 112 | + |
| 113 | + ValidUserServiceAccountTokenScopes = ValidPersonalTokenScopes |
| 114 | + |
| 115 | + ValidGroupServiceAccountTokenScopes = ValidGroupTokenScopes |
| 116 | + |
| 117 | + ValidPipelineProjectTokenScopes []string |
| 118 | + |
| 119 | + ValidProjectDeployTokenScopes = []string{ |
| 120 | + ScopeReadRepository.String(), |
| 121 | + ScopeReadRegistry.String(), |
| 122 | + ScopeWriteRegistry.String(), |
| 123 | + ScopeReadVirtualRegistry.String(), |
| 124 | + ScopeWriteVirtualRegistry.String(), |
| 125 | + ScopeReadPackageRegistry.String(), |
| 126 | + ScopeWritePackageRegistry.String(), |
| 127 | + } |
| 128 | + |
| 129 | + ValidGroupDeployTokenScopes = []string{ |
| 130 | + ScopeReadRepository.String(), |
| 131 | + ScopeReadRegistry.String(), |
| 132 | + ScopeWriteRegistry.String(), |
| 133 | + ScopeReadVirtualRegistry.String(), |
| 134 | + ScopeWriteVirtualRegistry.String(), |
| 135 | + ScopeReadPackageRegistry.String(), |
| 136 | + ScopeWritePackageRegistry.String(), |
| 137 | + } |
| 138 | +) |
| 139 | + |
| 140 | +func (i Scope) String() string { |
| 141 | + return string(i) |
| 142 | +} |
| 143 | + |
| 144 | +func (i Scope) Value() string { |
| 145 | + return i.String() |
| 146 | +} |
| 147 | + |
| 148 | +func ScopeParse(value string) (Scope, error) { |
| 149 | + if slices.Contains(ValidGroupTokenScopes, value) || |
| 150 | + slices.Contains(ValidPipelineProjectTokenScopes, value) || |
| 151 | + slices.Contains(ValidGroupDeployTokenScopes, value) || |
| 152 | + slices.Contains(ValidProjectDeployTokenScopes, value) || |
| 153 | + slices.Contains(ValidPersonalTokenScopes, value) || |
| 154 | + slices.Contains(ValidProjectTokenScopes, value) || |
| 155 | + slices.Contains(ValidUserServiceAccountTokenScopes, value) || |
| 156 | + slices.Contains(ValidGroupServiceAccountTokenScopes, value) { |
| 157 | + return Scope(value), nil |
| 158 | + } |
| 159 | + return ScopeUnknown, fmt.Errorf("failed to parse '%s': %w", value, errs.ErrUnknownTokenScope) |
| 160 | +} |
0 commit comments