| 1 | package connection |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "errors" |
| 8 | "io" |
| 9 | "net/http" |
| 10 | "net/url" |
| 11 | "time" |
| 12 | |
| 13 | access "amadan.net/oficina/agent-access.git" |
| 14 | "amadan.net/oficina/agent-access.git/grant" |
| 15 | ) |
| 16 | |
| 17 | type DirectoryApp struct { |
| 18 | App string `json:"app"` |
| 19 | Origin string `json:"origin"` |
| 20 | } |
| 21 | type DirectoryTeam struct { |
| 22 | ID string `json:"id"` |
| 23 | Name string `json:"name"` |
| 24 | Apps []DirectoryApp `json:"apps"` |
| 25 | } |
| 26 | type Directory struct { |
| 27 | origin, token string |
| 28 | client *http.Client |
| 29 | } |
| 30 | |
| 31 | func validOrigin(raw string) bool { |
| 32 | u, e := url.Parse(raw) |
| 33 | return e == nil && u.Scheme == "https" && u.Host != "" && u.User == nil && u.Path == "" && u.RawQuery == "" && u.Fragment == "" && !u.ForceQuery |
| 34 | } |
| 35 | func NewDirectory(origin, token string, transport http.RoundTripper) (*Directory, error) { |
| 36 | if !validOrigin(origin) || len(token) < 32 || len(token) > 256 { |
| 37 | return nil, errors.New("invalid directory connection configuration") |
| 38 | } |
| 39 | return &Directory{origin, token, &http.Client{Transport: transport, Timeout: 10 * time.Second, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}}, nil |
| 40 | } |
| 41 | func (d *Directory) Lookup(ctx context.Context, subject string) ([]DirectoryTeam, error) { |
| 42 | raw, _ := json.Marshal(map[string]string{"subject": subject}) |
| 43 | req, err := http.NewRequestWithContext(ctx, "POST", d.origin+"/api/agent/directory", bytes.NewReader(raw)) |
| 44 | if err != nil { |
| 45 | return nil, access.Fail("unavailable", "Directory is unavailable.") |
| 46 | } |
| 47 | req.Header.Set("Authorization", "Bearer "+d.token) |
| 48 | req.Header.Set("Content-Type", "application/json") |
| 49 | resp, err := d.client.Do(req) |
| 50 | if err != nil { |
| 51 | return nil, access.Fail("unavailable", "Directory is unavailable.") |
| 52 | } |
| 53 | defer resp.Body.Close() |
| 54 | if resp.StatusCode != 200 { |
| 55 | return nil, access.Fail("unavailable", "Directory is unavailable.") |
| 56 | } |
| 57 | body, err := io.ReadAll(io.LimitReader(resp.Body, (1<<20)+1)) |
| 58 | var out struct { |
| 59 | Teams []DirectoryTeam `json:"teams"` |
| 60 | } |
| 61 | if err != nil || len(body) > 1<<20 || access.DecodeJSON(body, &out) != nil { |
| 62 | return nil, access.Fail("unavailable", "Directory returned an invalid response.") |
| 63 | } |
| 64 | teams := map[string]bool{} |
| 65 | for _, team := range out.Teams { |
| 66 | if team.ID == "" || len(team.ID) > 64 || team.Name == "" || len(team.Name) > 1024 || teams[team.ID] { |
| 67 | return nil, access.Fail("unavailable", "Directory returned invalid team data.") |
| 68 | } |
| 69 | teams[team.ID] = true |
| 70 | apps := map[string]bool{} |
| 71 | for _, app := range team.Apps { |
| 72 | if app.App == "" || apps[app.App] || !validOrigin(app.Origin) { |
| 73 | return nil, access.Fail("unavailable", "Directory returned invalid app data.") |
| 74 | } |
| 75 | apps[app.App] = true |
| 76 | } |
| 77 | } |
| 78 | return out.Teams, nil |
| 79 | } |
| 80 | |
| 81 | type credentials struct { |
| 82 | directory *Directory |
| 83 | store *grant.Store |
| 84 | routes []access.Route |
| 85 | } |
| 86 | |
| 87 | func (c *credentials) teams(ctx context.Context, subject string) ([]grant.Team, error) { |
| 88 | return c.resourceTeams(ctx, subject, "") |
| 89 | } |
| 90 | |
| 91 | func (c *credentials) resourceTeams(ctx context.Context, subject, resource string) ([]grant.Team, error) { |
| 92 | teams, err := c.directory.Lookup(ctx, subject) |
| 93 | if err != nil { |
| 94 | return nil, err |
| 95 | } |
| 96 | out := []grant.Team{} |
| 97 | for _, team := range teams { |
| 98 | found := false |
| 99 | for _, app := range team.Apps { |
| 100 | for _, route := range c.routes { |
| 101 | if route.Team == team.ID && route.Resource == app.Origin+"/api/v1" && (resource == "" || resource == route.Resource) { |
| 102 | found = true |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | if found { |
| 107 | out = append(out, grant.Team{ID: team.ID, Name: team.Name}) |
| 108 | } |
| 109 | } |
| 110 | return out, nil |
| 111 | } |
| 112 | func (c *credentials) Downstream(ctx context.Context, p access.Principal, resource, team string, scopes []string) (string, error) { |
| 113 | teams, err := c.resourceTeams(ctx, p.Subject, resource) |
| 114 | if err != nil { |
| 115 | return "", err |
| 116 | } |
| 117 | for _, entry := range teams { |
| 118 | if entry.ID == team { |
| 119 | return c.store.Downstream(ctx, p, resource, team, scopes) |
| 120 | } |
| 121 | } |
| 122 | return "", access.Fail("forbidden", "The app is no longer available to this team.") |
| 123 | } |
| 124 | |