package api import ( "crypto/sha256" "encoding/hex" "encoding/json" "errors" "io" "net/http" "os" "path/filepath" "regexp" "strings" "drmdecryption/apps/streamd/internal/auth" "drmdecryption/modhost" "drmdecryption/repo" ) // moduleInfo is one modules// entry for agent sync. type moduleInfo struct { Name string `json:"name"` HasYAML bool `json:"has_yaml"` YAMLSHA256 string `json:"yaml_sha256,omitempty"` } var moduleNameRe = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9_-]{0,63}$`) func (h *Handler) modules(w http.ResponseWriter, r *http.Request) { path := strings.TrimPrefix(r.URL.Path, "/api/modules") path = strings.Trim(path, "/") // Module metadata describes the provider plugins installed on this host, // so even reading it needs a principal. if _, ok := auth.Guard(w, r, auth.RoleViewer); !ok { return } switch { case path == "" && r.Method == http.MethodGet: h.listModules(w, r) case path != "" && !strings.Contains(path, "/") && r.Method == http.MethodGet: h.getModule(w, r, path) case path != "" && !strings.Contains(path, "/") && r.Method == http.MethodPut: h.putModule(w, r, path) default: writeErr(w, http.StatusMethodNotAllowed, "method not allowed") } } func (h *Handler) listModules(w http.ResponseWriter, r *http.Request) { root := repo.Root() dir := modhost.ModulesDir(root) ents, err := os.ReadDir(dir) if err != nil { if os.IsNotExist(err) { writeJSON(w, http.StatusOK, map[string]any{"modules": []moduleInfo{}}) return } writeErr(w, http.StatusInternalServerError, err.Error()) return } out := []moduleInfo{} for _, e := range ents { if !e.IsDir() || strings.HasPrefix(e.Name(), ".") { continue } info := moduleYAMLInfo(filepath.Join(dir, e.Name()), e.Name()) out = append(out, info) } writeJSON(w, http.StatusOK, map[string]any{"modules": out}) } func (h *Handler) getModule(w http.ResponseWriter, r *http.Request, name string) { if !moduleNameRe.MatchString(name) { writeErr(w, http.StatusBadRequest, "invalid module name") return } info := moduleYAMLInfo(filepath.Join(modhost.ModulesDir(repo.Root()), name), name) writeJSON(w, http.StatusOK, info) } func (h *Handler) putModule(w http.ResponseWriter, r *http.Request, name string) { // Writing a module.yaml changes how streams are captured on this host: // operator, and recorded. p, ok := auth.Guard(w, r, auth.RoleOperator) if !ok { return } if !moduleNameRe.MatchString(name) { writeErr(w, http.StatusBadRequest, "invalid module name") return } body, err := io.ReadAll(io.LimitReader(r.Body, 2<<20)) if err != nil { writeErr(w, http.StatusBadRequest, err.Error()) return } defer r.Body.Close() yamlBytes, err := extractModuleYAML(r.Header.Get("Content-Type"), body) if err != nil { writeErr(w, http.StatusBadRequest, err.Error()) return } if len(strings.TrimSpace(string(yamlBytes))) == 0 { writeErr(w, http.StatusBadRequest, "empty module.yaml") return } modDir := filepath.Join(modhost.ModulesDir(repo.Root()), name) if err := os.MkdirAll(modDir, 0o755); err != nil { writeErr(w, http.StatusInternalServerError, err.Error()) return } dest := filepath.Join(modDir, "module.yaml") tmp := dest + ".tmp" if err := os.WriteFile(tmp, yamlBytes, 0o644); err != nil { writeErr(w, http.StatusInternalServerError, err.Error()) return } if err := os.Rename(tmp, dest); err != nil { _ = os.Remove(tmp) writeErr(w, http.StatusInternalServerError, err.Error()) return } info := moduleYAMLInfo(modDir, name) h.audit(r, p, http.StatusOK, "uploaded module.yaml for "+name) h.logger().Info("module.yaml updated", "module", name, "by", p.Actor(), "sha256", info.YAMLSHA256) writeJSON(w, http.StatusOK, map[string]any{ "ok": true, "module": info, }) } func moduleYAMLInfo(modDir, name string) moduleInfo { info := moduleInfo{Name: name} b, err := os.ReadFile(filepath.Join(modDir, "module.yaml")) if err != nil { return info } sum := sha256.Sum256(b) info.HasYAML = true info.YAMLSHA256 = hex.EncodeToString(sum[:]) return info } // extractModuleYAML accepts raw YAML or JSON {"yaml":"..."}. func extractModuleYAML(contentType string, body []byte) ([]byte, error) { ct := strings.ToLower(strings.TrimSpace(strings.Split(contentType, ";")[0])) switch ct { case "application/json", "text/json": var wrap struct { YAML string `json:"yaml"` } if err := json.Unmarshal(body, &wrap); err != nil { return nil, err } if strings.TrimSpace(wrap.YAML) == "" { return nil, errors.New("empty yaml field") } return []byte(wrap.YAML), nil default: // text/yaml, application/x-yaml, or bare body return body, nil } }