forked from juanfont/headscale
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat: implements APIs for managing ACLs
Headscale currently lacks the APIs to manange the ACLs. The only way possible currently is to load the ACLs via file and changes to the policy requires reloading the headscale process. This also makes it difficult to integrate your headscale via APIs with no ACL management. This commit introduces two APIs that allow your to get and set the policy.
- Loading branch information
1 parent
a244eab
commit 4331948
Showing
32 changed files
with
1,628 additions
and
465 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,3 +45,6 @@ integration_test/etc/config.dump.yaml | |
/site | ||
|
||
__debug_bin | ||
|
||
acl.json | ||
|
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,94 @@ | ||
package cli | ||
|
||
import ( | ||
"io" | ||
"os" | ||
|
||
"github.com/rs/zerolog/log" | ||
"github.com/spf13/cobra" | ||
|
||
v1 "github.com/juanfont/headscale/gen/go/headscale/v1" | ||
"github.com/juanfont/headscale/hscontrol/types" | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(aclCmd) | ||
aclCmd.AddCommand(getACL) | ||
|
||
setACL.Flags().StringP("policy", "p", "", "Path to a policy file in JSON format") | ||
if err := setACL.MarkFlagRequired("policy"); err != nil { | ||
log.Fatal().Err(err).Msg("") | ||
} | ||
aclCmd.AddCommand(setACL) | ||
} | ||
|
||
var aclCmd = &cobra.Command{ | ||
Use: "acl", | ||
Short: "Manage the Headscale ACL Policy", | ||
Aliases: []string{"acl"}, | ||
} | ||
|
||
var getACL = &cobra.Command{ | ||
Use: "get", | ||
Short: "Print the current ACL Policy JSON", | ||
Aliases: []string{"show", "view", "fetch"}, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
ctx, client, conn, cancel := getHeadscaleCLIClient() | ||
defer cancel() | ||
defer conn.Close() | ||
|
||
request := &v1.GetACLRequest{} | ||
|
||
response, err := client.GetACL(ctx, request) | ||
if err != nil { | ||
log.Fatal().Err(err).Msg("Cannot get ACL Policy") | ||
|
||
return | ||
} | ||
|
||
SuccessOutput(response.GetPolicy(), "", "json") | ||
}, | ||
} | ||
|
||
var setACL = &cobra.Command{ | ||
Use: "set", | ||
Short: "Updates the ACL Policy", | ||
Long: ` | ||
Updates the existing ACL Policy with the provided policy. The policy must be a valid JSON object. | ||
This command only works when the acl.policy_mode is set to "db", and the policy will be stored in the database.`, | ||
Aliases: []string{"put", "update"}, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
policyPath, _ := cmd.Flags().GetString("policy") | ||
|
||
f, err := os.Open(policyPath) | ||
if err != nil { | ||
log.Fatal().Err(err).Msg("Error opening the policy file") | ||
|
||
return | ||
} | ||
defer f.Close() | ||
|
||
policyBytes, err := io.ReadAll(f) | ||
if err != nil { | ||
log.Fatal().Err(err).Msg("Error reading the policy file") | ||
|
||
return | ||
} | ||
|
||
acl := types.ACL{Policy: policyBytes} | ||
|
||
request := &v1.SetACLRequest{Policy: acl.Proto().GetPolicy()} | ||
|
||
ctx, client, conn, cancel := getHeadscaleCLIClient() | ||
defer cancel() | ||
defer conn.Close() | ||
|
||
if _, err := client.SetACL(ctx, request); err != nil { | ||
log.Fatal().Err(err).Msg("Failed to set ACL Policy") | ||
|
||
return | ||
} | ||
|
||
SuccessOutput(nil, "ACL Policy updated.", "") | ||
}, | ||
} |
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
Oops, something went wrong.