perkeep/pkg/app/app.go

89 lines
2.5 KiB
Go
Raw Normal View History

/*
Copyright 2014 The Perkeep Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package app provides helpers for server applications interacting
// with Perkeep.
// See also https://camlistore.org/doc/app-environment for the related
// variables.
package app // import "perkeep.org/pkg/app"
import (
"errors"
"fmt"
"net/http"
"os"
"strings"
"perkeep.org/internal/httputil"
"perkeep.org/pkg/auth"
"perkeep.org/pkg/client"
)
// Auth returns the auth mode for the app to access Perkeep, as defined by
// environment variables automatically supplied by the Perkeep server host.
pkg/server/app: proxy search requests for publisher Some of the publisher features have moved from the server-side app to the client-side app (the browser) thanks to gopherjs. Some of these features imply doing some search queries against Camlistore, which requires authentication. The server-side app receives the necessary credentials on creation, from Camlistore. However, we can't just communicate them to the client-side (as we do with the web UI) since the publisher app itself does not require any auth and is supposed to be exposed to the world. Therefore, we need to allow some search queries to be done without authentication. To this end, the app handler on Camlistore now assumes a new role: it is also a search proxy for the app. The app sends an unauthenticated search query to the app handler (instead of directly to the search handler), and it is the role of the app handler to verify that this query is allowed for the app, and if yes, to forward the search to the Camlistore's search handler. We introduce a new mechanism to filter the search queries in the form of a master query. Upon startup, the publisher registers, using the new CAMLI_APP_MASTERQUERY_URL env var, a *search.SearchQuery with the app handler. The app handler runs that query and caches all the blob refs included in the response to that query. In the following, all incoming search queries are run by the app handler, which checks that none of the response blobs are out of the set defined by the aforementioned cached blob refs. If that check fails, the search response is not forwarded to the app/client. The process can be improved in a subsequent CL (or patchset), with finer grained domains, i.e. a master search query per published camliPath, instead of one for the whole app handler. Change-Id: I00d91ff73e0cbe78744bfae9878077dc3a8521f4
2016-07-20 15:54:29 +00:00
func Auth() (auth.AuthMode, error) {
return basicAuth()
}
func basicAuth() (auth.AuthMode, error) {
authString := os.Getenv("CAMLI_AUTH")
if authString == "" {
return nil, errors.New("CAMLI_AUTH var not set")
}
userpass := strings.Split(authString, ":")
if len(userpass) != 2 {
return nil, fmt.Errorf("invalid auth string syntax. got %q, want \"username:password\"", authString)
}
return auth.NewBasicAuth(userpass[0], userpass[1]), nil
}
// Client returns a Perkeep client as defined by environment variables
// automatically supplied by the Perkeep server host.
func Client() (*client.Client, error) {
server := os.Getenv("CAMLI_API_HOST")
if server == "" {
return nil, errors.New("CAMLI_API_HOST var not set")
}
am, err := basicAuth()
if err != nil {
return nil, err
}
return client.New(
client.OptionNoExternalConfig(),
client.OptionServer(server),
client.OptionAuthMode(am),
)
}
// ListenAddress returns the host:[port] network address, derived from the environment,
// that the application should listen on.
func ListenAddress() (string, error) {
listenAddr := os.Getenv("CAMLI_APP_LISTEN")
if listenAddr == "" {
return "", errors.New("CAMLI_APP_LISTEN is undefined")
}
return listenAddr, nil
}
// PathPrefix returns the app's prefix on the app handler if the request was proxied
// through Perkeep, or "/" if the request went directly to the app.
func PathPrefix(r *http.Request) string {
if prefix := httputil.PathBase(r); prefix != "" {
return prefix
}
return "/"
}