feature: Basic auth support with fission router (#2292)
This commit is contained in:
@@ -18,6 +18,7 @@ package function
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
@@ -178,6 +179,11 @@ func doHTTPRequest(ctx context.Context, url string, headers []string, method, bo
|
||||
return nil, errors.Wrap(err, "error creating HTTP request")
|
||||
}
|
||||
|
||||
accesstoken, ok := os.LookupEnv(util.FISSION_AUTH_TOKEN)
|
||||
if ok && len(accesstoken) != 0 {
|
||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %v", accesstoken))
|
||||
}
|
||||
|
||||
for _, header := range headers {
|
||||
headerKeyValue := strings.SplitN(header, ":", 2)
|
||||
if len(headerKeyValue) != 2 {
|
||||
@@ -185,6 +191,7 @@ func doHTTPRequest(ctx context.Context, url string, headers []string, method, bo
|
||||
}
|
||||
req.Header.Set(headerKeyValue[0], headerKeyValue[1])
|
||||
}
|
||||
|
||||
hc := &http.Client{Transport: otelhttp.NewTransport(http.DefaultTransport)}
|
||||
resp, err := hc.Do(req.WithContext(ctx))
|
||||
if err != nil {
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
Copyright 2022 The Fission 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 token
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
wrapper "github.com/fission/fission/pkg/fission-cli/cliwrapper/driver/cobra"
|
||||
"github.com/fission/fission/pkg/fission-cli/flag"
|
||||
)
|
||||
|
||||
func Commands() *cobra.Command {
|
||||
createCmd := &cobra.Command{
|
||||
Use: "create",
|
||||
Short: "Create a JWT token for function invocation",
|
||||
RunE: wrapper.Wrapper(Create),
|
||||
}
|
||||
wrapper.SetFlags(createCmd, flag.FlagSet{
|
||||
Required: []flag.Flag{flag.TokUsername, flag.TokPassword},
|
||||
Optional: []flag.Flag{flag.TokAuthURI},
|
||||
})
|
||||
|
||||
command := &cobra.Command{
|
||||
Use: "token",
|
||||
Short: "Create a JWT token for function invocation",
|
||||
}
|
||||
|
||||
command.AddCommand(createCmd)
|
||||
|
||||
return command
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
/*
|
||||
Copyright 2022 The Fission 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 token
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
fv1 "github.com/fission/fission/pkg/apis/core/v1"
|
||||
"github.com/fission/fission/pkg/fission-cli/cliwrapper/cli"
|
||||
"github.com/fission/fission/pkg/fission-cli/cmd"
|
||||
flagkey "github.com/fission/fission/pkg/fission-cli/flag/key"
|
||||
"github.com/fission/fission/pkg/fission-cli/util"
|
||||
)
|
||||
|
||||
type CreateSubCommand struct {
|
||||
cmd.CommandActioner
|
||||
}
|
||||
|
||||
func Create(input cli.Input) error {
|
||||
return (&CreateSubCommand{}).do(input)
|
||||
}
|
||||
|
||||
func (opts *CreateSubCommand) do(input cli.Input) error {
|
||||
return opts.run(input)
|
||||
}
|
||||
|
||||
func (opts *CreateSubCommand) run(input cli.Input) error {
|
||||
|
||||
lb := &fv1.AuthLogin{}
|
||||
|
||||
username := input.String(flagkey.TokUsername)
|
||||
if len(username) != 0 {
|
||||
lb.Username = username
|
||||
}
|
||||
|
||||
password := input.String(flagkey.TokPassword)
|
||||
if len(password) != 0 {
|
||||
lb.Password = password
|
||||
}
|
||||
|
||||
values := map[string]string{"username": username, "password": password}
|
||||
|
||||
jsonValue, _ := json.Marshal(values)
|
||||
|
||||
kubeContext := input.String(flagkey.KubeContext)
|
||||
// Portforward to the fission router
|
||||
localRouterPort, err := util.SetupPortForward(util.GetFissionNamespace(), "application=fission-router", kubeContext)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
authURI, _ := os.LookupEnv("FISSION_AUTH_URI")
|
||||
|
||||
if input.IsSet(flagkey.TokAuthURI) {
|
||||
authURI = input.String(flagkey.TokAuthURI)
|
||||
}
|
||||
|
||||
if len(authURI) == 0 {
|
||||
authURI = util.FISSION_AUTH_URI
|
||||
}
|
||||
|
||||
relativeURL, _ := url.Parse(authURI)
|
||||
serverURL, _ := url.Parse("http://127.0.0.1:" + localRouterPort)
|
||||
authAuthenticatorUrl := serverURL.ResolveReference(relativeURL)
|
||||
|
||||
resp, err := http.Post(authAuthenticatorUrl.String(), "application/json", bytes.NewBuffer(jsonValue))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error creating token")
|
||||
}
|
||||
|
||||
if resp.StatusCode == http.StatusCreated {
|
||||
var rat fv1.RouterAuthToken
|
||||
err = json.Unmarshal(body, &rat)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(rat.AccessToken)
|
||||
} else if resp.StatusCode == http.StatusNotFound {
|
||||
fmt.Printf("%s. Please check if authentication is enabled and correct auth URI is mentioned via --authuri or FISSION_AUTH_URI.\n", resp.Status)
|
||||
} else {
|
||||
fmt.Println(resp.Status)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -142,6 +142,10 @@ var (
|
||||
HtPrefix = Flag{Type: String, Name: flagkey.HtPrefix, Usage: "Prefix with which functions are exposed. NOTE: Prefix takes precedence over URL/RelativeURL [DEPRECATED for 'fn create', use 'route create' instead]"}
|
||||
HtKeepPrefix = Flag{Type: Bool, Name: flagkey.HtKeepPrefix, Usage: "Keep the prefix in the URL while forwarding request to the function"}
|
||||
|
||||
TokUsername = Flag{Type: String, Name: flagkey.TokUsername, Usage: "Username to generate token for function invocation"}
|
||||
TokPassword = Flag{Type: String, Name: flagkey.TokPassword, Usage: "Password to generate token for function invocation"}
|
||||
TokAuthURI = Flag{Type: String, Name: flagkey.TokAuthURI, Usage: "Relative URI path to generate token"}
|
||||
|
||||
TtName = Flag{Type: String, Name: flagkey.TtName, Usage: "Time Trigger name"}
|
||||
TtCron = Flag{Type: String, Name: flagkey.TtCron, Usage: "Time trigger cron spec with each asterisk representing respectively second, minute, hour, the day of the month, month and day of the week. Also supports readable formats like '@every 5m', '@hourly'"}
|
||||
TtFnName = Flag{Type: String, Name: flagkey.TtFnName, Usage: "Function name"}
|
||||
|
||||
@@ -92,6 +92,10 @@ const (
|
||||
HtPrefix = "prefix"
|
||||
HtKeepPrefix = "keepprefix"
|
||||
|
||||
TokUsername = "username"
|
||||
TokPassword = "password"
|
||||
TokAuthURI = "authuri"
|
||||
|
||||
TtName = resourceName
|
||||
TtCron = "cron"
|
||||
TtFnName = "function"
|
||||
|
||||
@@ -18,6 +18,8 @@ package util
|
||||
|
||||
// fission-cli options
|
||||
const (
|
||||
SPEC_IGNORE_FILE = ".specignore"
|
||||
COMMIT_LABEL = "commit"
|
||||
SPEC_IGNORE_FILE = ".specignore"
|
||||
COMMIT_LABEL = "commit"
|
||||
FISSION_AUTH_URI = "/auth/login"
|
||||
FISSION_AUTH_TOKEN = "FISSION_AUTH_TOKEN"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user