This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"git.cin.su/m/Assistant/contracts/identity"
|
||||
)
|
||||
|
||||
var (
|
||||
errCreatorSessionDenied = errors.New("creator session denied")
|
||||
errIdentityUnavailable = errors.New("identity service unavailable")
|
||||
)
|
||||
|
||||
type creatorIdentityClient struct {
|
||||
baseURL *url.URL
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
func newCreatorIdentityClient(rawURL string, client *http.Client) (creatorIdentityClient, error) {
|
||||
baseURL, err := url.Parse(rawURL)
|
||||
if err != nil || baseURL.Scheme == "" || baseURL.Host == "" {
|
||||
return creatorIdentityClient{}, fmt.Errorf("invalid Core Runtime URL")
|
||||
}
|
||||
return creatorIdentityClient{baseURL: baseURL, client: client}, nil
|
||||
}
|
||||
|
||||
func (c creatorIdentityClient) Session(ctx context.Context, authorization string) (identity.CreatorSession, error) {
|
||||
if !strings.HasPrefix(authorization, "Bearer ") || len(strings.TrimPrefix(authorization, "Bearer ")) == 0 {
|
||||
return identity.CreatorSession{}, errCreatorSessionDenied
|
||||
}
|
||||
target := *c.baseURL
|
||||
target.Path = "/v1/creator/sessions/current"
|
||||
target.RawQuery = ""
|
||||
request, err := http.NewRequestWithContext(ctx, http.MethodGet, target.String(), nil)
|
||||
if err != nil {
|
||||
return identity.CreatorSession{}, fmt.Errorf("%w: build request", errIdentityUnavailable)
|
||||
}
|
||||
request.Header.Set("Authorization", authorization)
|
||||
response, err := c.client.Do(request)
|
||||
if err != nil {
|
||||
return identity.CreatorSession{}, fmt.Errorf("%w: %v", errIdentityUnavailable, err)
|
||||
}
|
||||
defer response.Body.Close()
|
||||
if response.StatusCode == http.StatusUnauthorized {
|
||||
return identity.CreatorSession{}, errCreatorSessionDenied
|
||||
}
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return identity.CreatorSession{}, fmt.Errorf("%w: status %d", errIdentityUnavailable, response.StatusCode)
|
||||
}
|
||||
var session identity.CreatorSession
|
||||
if err := json.NewDecoder(io.LimitReader(response.Body, 16<<10)).Decode(&session); err != nil {
|
||||
return identity.CreatorSession{}, fmt.Errorf("%w: invalid response", errIdentityUnavailable)
|
||||
}
|
||||
if session.ActorID == "" || session.PrincipalID == "" {
|
||||
return identity.CreatorSession{}, fmt.Errorf("%w: incomplete session", errIdentityUnavailable)
|
||||
}
|
||||
return session, nil
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"git.cin.su/m/Assistant/contracts/identity"
|
||||
)
|
||||
|
||||
func TestCreatorIdentityClientValidatesThroughCore(t *testing.T) {
|
||||
core := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/v1/creator/sessions/current" || r.Header.Get("Authorization") != "Bearer bounded" {
|
||||
t.Fatal("unexpected Identity request")
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(identity.CreatorSession{ActorID: "actor", PrincipalID: "principal", ExpiresAtUnix: 100})
|
||||
}))
|
||||
defer core.Close()
|
||||
client, err := newCreatorIdentityClient(core.URL, core.Client())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
session, err := client.Session(context.Background(), "Bearer bounded")
|
||||
if err != nil || session.PrincipalID != "principal" {
|
||||
t.Fatalf("session = %+v, err = %v", session, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreatorIdentityClientDistinguishesDenialFromOutage(t *testing.T) {
|
||||
denyingCore := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
http.Error(w, "denied", http.StatusUnauthorized)
|
||||
}))
|
||||
defer denyingCore.Close()
|
||||
client, err := newCreatorIdentityClient(denyingCore.URL, denyingCore.Client())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := client.Session(context.Background(), "Bearer invalid"); !errors.Is(err, errCreatorSessionDenied) {
|
||||
t.Fatalf("denial error = %v", err)
|
||||
}
|
||||
|
||||
unavailableCore := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
http.Error(w, "unavailable", http.StatusServiceUnavailable)
|
||||
}))
|
||||
defer unavailableCore.Close()
|
||||
client, err = newCreatorIdentityClient(unavailableCore.URL, unavailableCore.Client())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := client.Session(context.Background(), "Bearer bounded"); !errors.Is(err, errIdentityUnavailable) {
|
||||
t.Fatalf("availability error = %v", err)
|
||||
}
|
||||
}
|
||||
@@ -3,12 +3,15 @@ package main
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"git.cin.su/m/Assistant/adapters/postgres"
|
||||
"git.cin.su/m/Assistant/contracts/continuity"
|
||||
"git.cin.su/m/Assistant/runtime/health"
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"git.cin.su/m/Assistant/adapters/postgres"
|
||||
"git.cin.su/m/Assistant/contracts/continuity"
|
||||
"git.cin.su/m/Assistant/runtime/health"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -21,6 +24,10 @@ func main() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer repository.Close()
|
||||
identityClient, err := newCreatorIdentityClient(envOr("CORE_RUNTIME_URL", "http://core-runtime:8080"), &http.Client{Timeout: 5 * time.Second})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
mux := http.NewServeMux()
|
||||
mux.Handle("/healthz", health.Handler{Runtime: "continuity-runtime", SemanticGeneration: 1, ArchitectureGeneration: 1})
|
||||
mux.HandleFunc("POST /v1/prospective-items", func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -44,8 +51,37 @@ func main() {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(item)
|
||||
})
|
||||
mux.HandleFunc("GET /v1/creator/prospective-items/{id}", func(w http.ResponseWriter, r *http.Request) {
|
||||
session, err := identityClient.Session(r.Context(), r.Header.Get("Authorization"))
|
||||
if errors.Is(err, errCreatorSessionDenied) {
|
||||
http.Error(w, "creator session denied", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
http.Error(w, "identity service unavailable", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
item, err := repository.Load(r.Context(), continuity.ProspectiveItemID(r.PathValue("id")))
|
||||
if err != nil {
|
||||
http.Error(w, "prospective item not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
if item.OwnerPrincipalID != session.PrincipalID {
|
||||
http.Error(w, "prospective item denied", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(item)
|
||||
})
|
||||
log.Printf("continuity-runtime listening on %s", addr)
|
||||
if err := http.ListenAndServe(addr, mux); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func envOr(name, fallback string) string {
|
||||
if value := os.Getenv(name); value != "" {
|
||||
return value
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user