add policies API.

This commit is contained in:
Miroslav Šedivý
2021-07-25 01:53:14 +02:00
parent 9912786146
commit 97445b11e4
5 changed files with 154 additions and 1 deletions
+29
View File
@@ -330,6 +330,8 @@ components:
type: array
items:
$ref: '#/components/schemas/RoomMount'
policies:
$ref: '#/components/schemas/Policies'
RoomStats:
type: object
@@ -360,3 +362,30 @@ components:
muted:
type: boolean
example: false
Policies:
type: object
properties:
homepage:
type: string
example: https://google.com
extensions:
type: array
items:
$ref: '#/components/schemas/Extension'
developer_tools:
type: boolean
example: true
persistent_data:
type: boolean
example: false
Extension:
type: object
properties:
id:
type: string
example: uBlock0@raymondhill.net
url:
type: string
example: https://addons.mozilla.org/firefox/downloads/latest/ublock-origin/latest.xpi
+1 -1
View File
@@ -1 +1 @@
5.2.0-SNAPSHOT
5.2.1-SNAPSHOT
+56
View File
@@ -21,6 +21,56 @@ import { DUMMY_BASE_URL, assertParamExists, setApiKeyToObject, setBasicAuthToObj
// @ts-ignore
import { BASE_PATH, COLLECTION_FORMATS, RequestArgs, BaseAPI, RequiredError } from './base';
/**
*
* @export
* @interface Extension
*/
export interface Extension {
/**
*
* @type {string}
* @memberof Extension
*/
id?: string;
/**
*
* @type {string}
* @memberof Extension
*/
url?: string;
}
/**
*
* @export
* @interface Policies
*/
export interface Policies {
/**
*
* @type {string}
* @memberof Policies
*/
homepage?: string;
/**
*
* @type {Array<Extension>}
* @memberof Policies
*/
extensions?: Array<Extension>;
/**
*
* @type {boolean}
* @memberof Policies
*/
developer_tools?: boolean;
/**
*
* @type {boolean}
* @memberof Policies
*/
persistent_data?: boolean;
}
/**
*
* @export
@@ -245,6 +295,12 @@ export interface RoomSettings {
* @memberof RoomSettings
*/
mounts?: Array<RoomMount>;
/**
*
* @type {Policies}
* @memberof RoomSettings
*/
policies?: Policies;
}
/**
*
+66
View File
@@ -21,6 +21,7 @@ import (
"github.com/rs/zerolog/log"
"m1k1o/neko_rooms/internal/config"
"m1k1o/neko_rooms/internal/policies"
"m1k1o/neko_rooms/internal/types"
"m1k1o/neko_rooms/internal/utils"
)
@@ -194,6 +195,47 @@ func (manager *RoomManagerCtx) Create(settings types.RoomSettings) (string, erro
env = append(env, fmt.Sprintf("NEKO_NAT1TO1=%s", strings.Join(manager.config.NAT1To1IPs, ",")))
}
// browser policies
if settings.Policies != nil {
if !manager.config.StorageEnabled {
return "", fmt.Errorf("policies cannot be specified, because storage is disabled or unavailable")
}
policyConfig := policies.GetConfig(settings.NekoImage)
if policyConfig == nil {
return "", fmt.Errorf("policy config for this image does not exist")
}
policyJson, err := policies.Generate(*settings.Policies, policyConfig.Type)
if err != nil {
return "", err
}
// create policy path (+ also get host path)
policyPath := fmt.Sprintf("/%s-%s-policy.json", roomName, policyConfig.Type)
templateInternalPath := path.Join(manager.config.StorageInternal, templateStoragePath)
policyInternalPath := path.Join(templateInternalPath, policyPath)
// create dir if does not exist
if _, err := os.Stat(templateInternalPath); os.IsNotExist(err) {
if err := os.MkdirAll(templateInternalPath, os.ModePerm); err != nil {
return "", err
}
}
// write policy to file
if err := os.WriteFile(policyInternalPath, []byte(policyJson), 0644); err != nil {
return "", err
}
// mount policy file
settings.Mounts = append(settings.Mounts, types.RoomMount{
Type: types.MountTemplate,
HostPath: policyPath,
ContainerPath: policyConfig.Path,
})
}
mounts := []dockerMount.Mount{}
for _, mount := range settings.Mounts {
readOnly := false
@@ -396,11 +438,35 @@ func (manager *RoomManagerCtx) GetSettings(id string) (*types.RoomSettings, erro
})
}
var policiesData *types.Policies
policyConfig := policies.GetConfig(nekoImage)
if policyConfig != nil {
var policyMount *types.RoomMount
for _, mount := range mounts {
if mount.ContainerPath == policyConfig.Path {
policyMount = &mount
break
}
}
// TODO: Refactor.
if policyMount != nil {
if _, err := os.Stat(policyMount.HostPath); !os.IsNotExist(err) {
if data, err := os.ReadFile(policyMount.HostPath); err == nil {
if pData, err := policies.Parse(string(data), policyConfig.Type); err == nil {
policiesData = pData
}
}
}
}
}
settings := types.RoomSettings{
Name: roomName,
NekoImage: nekoImage,
MaxConnections: epr.Max - epr.Min + 1,
Mounts: mounts,
Policies: policiesData,
}
err = settings.FromEnv(container.Config.Env)
+2
View File
@@ -75,6 +75,8 @@ type RoomSettings struct {
Envs map[string]string `json:"envs"`
Mounts []RoomMount `json:"mounts"`
Policies *Policies `json:"policies,omitempty"`
}
func (settings *RoomSettings) ToEnv() []string {