Require access to Still via method

This commit adds a safeguard to accessing the still from a specific
component by requiring access via the component.GetStill method.
This commit is contained in:
Tom Wiesing 2024-04-08 22:39:32 +02:00
parent 81fa84c244
commit 8235ea9105
No known key found for this signature in database
63 changed files with 288 additions and 197 deletions

View file

@ -9,6 +9,7 @@ import (
"net/http"
"time"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/tkw1536/pkglib/timex"
@ -58,12 +59,12 @@ func (rh *RequestHeaders) With(headers RequestHeaders) *RequestHeaders {
}
// DoRest performs a (raw) http request to the without a body.
func (ts Triplestore) DoRest(ctx context.Context, timeout time.Duration, method, url string, headers *RequestHeaders) (*http.Response, error) {
func (ts *Triplestore) DoRest(ctx context.Context, timeout time.Duration, method, url string, headers *RequestHeaders) (*http.Response, error) {
return ts.DoRestWithReader(ctx, timeout, method, url, headers, nil)
}
// DoRestWithForm performs a http request where the body are all bytes read from fieldvalue.
func (ts Triplestore) DoRestWithForm(ctx context.Context, timeout time.Duration, method, url string, headers *RequestHeaders, fieldname string, fieldvalue io.Reader) (*http.Response, error) {
func (ts *Triplestore) DoRestWithForm(ctx context.Context, timeout time.Duration, method, url string, headers *RequestHeaders, fieldname string, fieldvalue io.Reader) (*http.Response, error) {
var buffer bytes.Buffer
// write the file to it
@ -83,7 +84,7 @@ func (ts Triplestore) DoRestWithForm(ctx context.Context, timeout time.Duration,
// DoRestWithReader performs a http request where the body is copied from the given io.Reader.
// The caller must ensure the reader is closed.
func (ts Triplestore) DoRestWithMarshal(ctx context.Context, timeout time.Duration, method, url string, headers *RequestHeaders, body any) (*http.Response, error) {
func (ts *Triplestore) DoRestWithMarshal(ctx context.Context, timeout time.Duration, method, url string, headers *RequestHeaders, body any) (*http.Response, error) {
// encode into a buffer
var buffer bytes.Buffer
if err := json.NewEncoder(&buffer).Encode(body); err != nil {
@ -95,7 +96,7 @@ func (ts Triplestore) DoRestWithMarshal(ctx context.Context, timeout time.Durati
// DoRestWithReader performs a http request where the body is copied from the given io.Reader.
// The caller must ensure the reader is closed.
func (ts Triplestore) DoRestWithReader(ctx context.Context, timeout time.Duration, method string, url string, headers *RequestHeaders, body io.Reader) (*http.Response, error) {
func (ts *Triplestore) DoRestWithReader(ctx context.Context, timeout time.Duration, method string, url string, headers *RequestHeaders, body io.Reader) (*http.Response, error) {
// create the request object
client := &http.Client{
Timeout: timeout,
@ -104,12 +105,14 @@ func (ts Triplestore) DoRestWithReader(ctx context.Context, timeout time.Duratio
},
}
config := component.GetStill(ts).Config.TS
// create the request and authentication
req, err := http.NewRequestWithContext(ctx, method, ts.BaseURL+url, body)
if err != nil {
return nil, err
}
req.SetBasicAuth(ts.Config.TS.AdminUsername, ts.Config.TS.AdminPassword)
req.SetBasicAuth(config.AdminUsername, config.AdminPassword)
// add extra headers
if headers != nil && headers.Accept != "" {

View file

@ -5,7 +5,7 @@ import (
"path/filepath"
"time"
"github.com/FAU-CDI/wisski-distillery/internal/config"
config_package "github.com/FAU-CDI/wisski-distillery/internal/config"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/tkw1536/pkglib/yamlx"
"gopkg.in/yaml.v3"
@ -27,7 +27,7 @@ var (
)
func (ts *Triplestore) Path() string {
return filepath.Join(ts.Still.Config.Paths.Root, "core", "triplestore")
return filepath.Join(component.GetStill(ts).Config.Paths.Root, "core", "triplestore")
}
func (Triplestore) Context(parent component.InstallationContext) component.InstallationContext {
@ -38,6 +38,7 @@ func (Triplestore) Context(parent component.InstallationContext) component.Insta
var resources embed.FS
func (ts *Triplestore) Stack() component.StackWithResources {
config := component.GetStill(ts).Config
return component.MakeStack(ts, component.StackWithResources{
Resources: resources,
ContextPath: "triplestore",
@ -45,14 +46,14 @@ func (ts *Triplestore) Stack() component.StackWithResources {
CopyContextFiles: []string{"graphdb.zip"}, // TODO: Move into constant?
EnvContext: map[string]string{
"DOCKER_NETWORK_NAME": ts.Config.Docker.Network(),
"HOST_RULE": ts.Config.HTTP.HostRule(config.TriplestoreDomain.Domain()),
"HTTPS_ENABLED": ts.Config.HTTP.HTTPSEnabledEnv(),
"DOCKER_NETWORK_NAME": config.Docker.Network(),
"HOST_RULE": config.HTTP.HostRule(config_package.TriplestoreDomain.Domain()),
"HTTPS_ENABLED": config.HTTP.HTTPSEnabledEnv(),
},
ComposerYML: func(root *yaml.Node) (*yaml.Node, error) {
// ts is exposed => everything is fine
if ts.Config.HTTP.TS.Set && ts.Config.HTTP.TS.Value {
if config.HTTP.TS.Set && config.HTTP.TS.Value {
return root, nil
}

View file

@ -6,13 +6,14 @@ import (
"io"
"net/http"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/FAU-CDI/wisski-distillery/pkg/logging"
"github.com/pkg/errors"
)
var errTriplestoreFailedSecurity = errors.New("failed to enable triplestore security: request did not succeed with HTTP 200 OK")
func (ts Triplestore) Update(ctx context.Context, progress io.Writer) error {
func (ts *Triplestore) Update(ctx context.Context, progress io.Writer) error {
logging.LogMessage(progress, "Waiting for Triplestore")
if err := ts.Wait(ctx); err != nil {
return err
@ -20,8 +21,10 @@ func (ts Triplestore) Update(ctx context.Context, progress io.Writer) error {
logging.LogMessage(progress, "Resetting admin user password")
{
res, err := ts.DoRestWithMarshal(ctx, tsTrivialTimeout, http.MethodPut, "/rest/security/users/"+ts.Config.TS.AdminUsername, nil, TriplestoreUserPayload{
Password: ts.Config.TS.AdminPassword,
config := component.GetStill(ts).Config.TS
res, err := ts.DoRestWithMarshal(ctx, tsTrivialTimeout, http.MethodPut, "/rest/security/users/"+config.AdminUsername, nil, TriplestoreUserPayload{
Password: config.AdminPassword,
AppSettings: TriplestoreUserAppSettings{
DefaultInference: true,
DefaultVisGraphSchema: true,