-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
331 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,55 @@ | ||
# Access Control | ||
|
||
Access control is configured by ACL rules of different types. A request/action | ||
passes the access control check if it matches any of the applicable ACL rules. | ||
|
||
A typical ACL would looks like this: | ||
```toml | ||
access = [ | ||
{ githubUser="username" }, | ||
{ ipRange="127.0.0.1/32" } | ||
] | ||
``` | ||
|
||
## Authentication | ||
|
||
A GitHub user may authenticate through the `pageship login` command. Currently, | ||
it will connect to the Pageship server through SSH protocol, and verify user's | ||
identity through GitHub user's public key. | ||
|
||
GitHub Actions jobs would be authenticate automatically when `pageship` command | ||
detected running in CI environment. It authenticates through GitHub Actions | ||
OIDC token. | ||
|
||
## ACL Types | ||
|
||
### GitHub user | ||
|
||
```toml | ||
{ githubUser = "username" } | ||
``` | ||
|
||
Actions/requests from the specified GitHub user is allowed. | ||
|
||
### GitHub Actions repository | ||
```toml | ||
{ gitHubRepositoryActions = "oursky/pageship" } | ||
{ gitHubRepositoryActions = "oursky/*" } | ||
{ gitHubRepositoryActions = "*" } | ||
``` | ||
|
||
Actions/requests from the specified GitHub Action jobs is allowed. Wildcard can | ||
be specified for all repository of a user/organization, or any repository. | ||
|
||
|
||
### IP Range | ||
|
||
```toml | ||
{ ipRange = "127.0.0.1/32" } | ||
{ ipRange = "192.168.0.0/16" } | ||
{ ipRange = "0.0.0.0/0" } | ||
{ ipRange = "::1/128" } | ||
``` | ||
|
||
Actions/requests from the specified IP range (CIDR) is allowed. | ||
IPv4 is mapped to IPv6 before matching. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package models | ||
|
||
import ( | ||
"encoding/hex" | ||
"net/netip" | ||
"strings" | ||
|
||
"github.com/oursky/pageship/internal/config" | ||
) | ||
|
||
type CredentialIndexKey string | ||
|
||
func MakeCredentialIDIndexKeys(id CredentialID) []CredentialIndexKey { | ||
kind, data, found := strings.Cut(string(id), ":") | ||
if !found { | ||
kind = "" | ||
} | ||
|
||
switch CredentialIDKind(kind) { | ||
case CredentialIDKindUserID, | ||
CredentialIDKindGitHubUser: | ||
return []CredentialIndexKey{CredentialIndexKey(id)} | ||
|
||
case CredentialIDGitHubRepositoryActions: | ||
owner, repo, ok := strings.Cut(data, "/") | ||
if !ok { | ||
return nil | ||
} | ||
prefix := string(CredentialIDGitHubRepositoryActions) + ":" | ||
return []CredentialIndexKey{ | ||
CredentialIndexKey(prefix + "*"), | ||
CredentialIndexKey(prefix + owner), | ||
CredentialIndexKey(prefix + owner + "/" + repo), | ||
} | ||
|
||
case CredentialIDIP: | ||
addr, err := netip.ParseAddr(data) | ||
if err != nil { | ||
return nil | ||
} | ||
|
||
addr = netip.AddrFrom16(addr.As16()) | ||
return makeIPKeys(addr, addr.BitLen()) | ||
|
||
default: | ||
return nil | ||
} | ||
} | ||
|
||
func CollectCredentialIDIndexKeys(ids []CredentialID) []CredentialIndexKey { | ||
var keys []CredentialIndexKey | ||
for _, id := range ids { | ||
keys = append(keys, MakeCredentialIDIndexKeys(id)...) | ||
} | ||
return keys | ||
} | ||
|
||
func MakeCredentialRuleIndexKeys(r *config.ACLSubjectRule) []CredentialIndexKey { | ||
switch { | ||
case r.PageshipUser != "": | ||
return MakeCredentialIDIndexKeys(CredentialUserID(r.PageshipUser)) | ||
case r.GitHubUser != "": | ||
return MakeCredentialIDIndexKeys(CredentialGitHubUser(r.GitHubUser)) | ||
case r.GitHubRepositoryActions != "": | ||
prefix := string(CredentialIDGitHubRepositoryActions) + ":" | ||
if r.GitHubRepositoryActions == "*" { | ||
return []CredentialIndexKey{CredentialIndexKey(prefix + "*")} | ||
} | ||
|
||
owner, repo, ok := strings.Cut(r.GitHubRepositoryActions, "/") | ||
if !ok { | ||
return nil | ||
} | ||
if repo == "*" { | ||
return []CredentialIndexKey{CredentialIndexKey(prefix + owner)} | ||
} else { | ||
return []CredentialIndexKey{CredentialIndexKey(prefix + owner + "/" + repo)} | ||
} | ||
case r.IpRange != "": | ||
cidr, err := netip.ParsePrefix(r.IpRange) | ||
if err != nil { | ||
return nil | ||
} | ||
|
||
bits := cidr.Bits() | ||
addr := netip.AddrFrom16(cidr.Addr().As16()) | ||
if cidr.Addr().Is4() { | ||
// Map ipv4 CIDR to ipv6. | ||
bits += 96 | ||
} | ||
|
||
keys := makeIPKeys(addr, bits) | ||
return []CredentialIndexKey{keys[len(keys)-1]} // Use longest key (i.e. last key) | ||
} | ||
return nil | ||
} | ||
|
||
func makeIPKeys(addr netip.Addr, bits int) []CredentialIndexKey { | ||
addrBytes := addr.As16() | ||
bytes := addrBytes[:(bits/8)&(^1)] | ||
|
||
keys := []CredentialIndexKey{"ip:"} | ||
var s strings.Builder | ||
for b := 0; b < len(bytes); b += 2 { | ||
s.WriteString(hex.EncodeToString(bytes[b : b+2])) | ||
keys = append(keys, CredentialIndexKey("ip:"+s.String())) | ||
} | ||
return keys | ||
} |
Oops, something went wrong.