-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2702 from openziti/add-verify-ext-jwt-oidc
Add verify ext jwt OIDC
- Loading branch information
Showing
15 changed files
with
769 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
Copyright NetFoundry Inc. | ||
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 | ||
https://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 cobra | ||
|
||
import ( | ||
"fmt" | ||
"github.com/openziti/ziti/ziti/cmd/consts" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
) | ||
|
||
func AddFlagAnnotation(cmd *cobra.Command, flagName string, key string, value string) error { | ||
flag := cmd.Flags().Lookup(flagName) | ||
if flag == nil { | ||
return fmt.Errorf("flag %q not found", flagName) | ||
} | ||
if flag.Annotations == nil { | ||
flag.Annotations = make(map[string][]string) | ||
} | ||
flag.Annotations[key] = append(flag.Annotations[key], value) | ||
return nil | ||
} | ||
|
||
func GetFlagsForAnnotation(cmd *cobra.Command, annotation string) string { | ||
var result string | ||
maxLength := MaxFlagNameLength(cmd) | ||
cmd.Flags().VisitAll(func(flag *pflag.Flag) { | ||
if flag.Annotations[annotation] != nil { | ||
if flag.Shorthand != "" { | ||
result += fmt.Sprintf(" -%s, --%-*s %s\n", flag.Shorthand, maxLength, flag.Name, flag.Usage) | ||
} else { | ||
result += fmt.Sprintf(" --%-*s %s\n", maxLength, flag.Name, flag.Usage) | ||
} | ||
} | ||
}) | ||
|
||
return result | ||
} | ||
|
||
func GetFlagsWithoutAnnotations(cmd *cobra.Command, annotations ...string) string { | ||
var result string | ||
maxLength := MaxFlagNameLength(cmd) | ||
|
||
// Create a map of the provided annotations for quick lookup | ||
annotationMap := make(map[string]bool) | ||
for _, annotation := range annotations { | ||
annotationMap[annotation] = true | ||
} | ||
|
||
cmd.Flags().VisitAll(func(flag *pflag.Flag) { | ||
hasAnnotation := false | ||
for ann := range flag.Annotations { | ||
if annotationMap[ann] { | ||
hasAnnotation = true | ||
break | ||
} | ||
} | ||
if !hasAnnotation { | ||
if flag.Shorthand != "" { | ||
result += fmt.Sprintf(" -%s, --%-*s %s\n", flag.Shorthand, maxLength, flag.Name, flag.Usage) | ||
} else { | ||
result += fmt.Sprintf(" --%-*s %s\n", maxLength, flag.Name, flag.Usage) | ||
} | ||
} | ||
}) | ||
|
||
return result | ||
} | ||
|
||
func MaxFlagNameLength(cmd *cobra.Command) int { | ||
// Calculate the maximum flag length across ALL flags | ||
maxLength := 0 | ||
cmd.Flags().VisitAll(func(flag *pflag.Flag) { | ||
length := len(flag.Name) | ||
if flag.Shorthand != "" { | ||
length += 4 // Account for "-x, " | ||
} | ||
if length > maxLength { | ||
maxLength = length | ||
} | ||
}) | ||
return maxLength | ||
} | ||
|
||
func SetHelpTemplate(cmd *cobra.Command) { | ||
l := GetFlagsForAnnotation(cmd, cmdconsts.LoginFlagKey) | ||
c := GetFlagsForAnnotation(cmd, cmdconsts.CommonFlagKey) | ||
u := GetFlagsWithoutAnnotations(cmd, cmdconsts.LoginFlagKey, cmdconsts.CommonFlagKey) | ||
|
||
cmd.SetHelpTemplate(`{{.Long}} | ||
Usage: | ||
{{.UseLine}} | ||
Available Commands: | ||
{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}} | ||
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}} | ||
Flags: | ||
` + u + ` | ||
Flags related to logging in: | ||
` + l + ` | ||
Common flags for all commands: | ||
` + c + ` | ||
Use "{{.CommandPath}} [command] --help" for more information about a command. | ||
`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
Copyright NetFoundry Inc. | ||
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 | ||
https://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 client | ||
|
||
import ( | ||
"context" | ||
"github.com/michaelquigley/pfxlog" | ||
"github.com/openziti/edge-api/rest_client_api_client" | ||
"github.com/openziti/edge-api/rest_client_api_client/external_jwt_signer" | ||
"github.com/openziti/edge-api/rest_model" | ||
internalconsts "github.com/openziti/ziti/internal/rest/consts" | ||
) | ||
|
||
func ExternalJWTSignerFromFilter(client *rest_client_api_client.ZitiEdgeClient, filter string) *rest_model.ClientExternalJWTSignerDetail { | ||
params := &external_jwt_signer.ListExternalJWTSignersParams{ | ||
Filter: &filter, | ||
Context: context.Background(), | ||
} | ||
params.SetTimeout(internalconsts.DefaultTimeout) | ||
resp, err := client.ExternalJWTSigner.ListExternalJWTSigners(params) | ||
if err != nil { | ||
pfxlog.Logger().Errorf("Could not obtain an ID for the external jwt signer with filter %s: %v", filter, err) | ||
return nil | ||
} | ||
if resp == nil || resp.Payload == nil || resp.Payload.Data == nil || len(resp.Payload.Data) == 0 { | ||
return nil | ||
} | ||
return resp.Payload.Data[0] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package internal_consts | ||
|
||
import "time" | ||
|
||
const ( | ||
DefaultTimeout = 5 * time.Second | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.