-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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 headscale policy
This commit introduces APIs for managing the headscale policy. Until now, the support was limited to file based policy and users could only update the file and reload headscale in order to apply the changes in the policy. With the new APIs, the users of headscale and now manage the policy programatically and will not require to restart headscale. The commit also implements the CLI commands to get and set the policy. BREAKING CHANGE: headscale no longer supports YAML policy files and the only supported format is HuJSON.
- Loading branch information
1 parent
4e7704d
commit f79a2f3
Showing
38 changed files
with
1,850 additions
and
563 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
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,91 @@ | ||
package cli | ||
|
||
import ( | ||
"io" | ||
"os" | ||
|
||
"github.com/rs/zerolog/log" | ||
"github.com/spf13/cobra" | ||
|
||
v1 "github.com/juanfont/headscale/gen/go/headscale/v1" | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(policyCmd) | ||
policyCmd.AddCommand(getPolicy) | ||
|
||
setPolicy.Flags().StringP("file", "f", "", "Path to a policy file in HuJSON format") | ||
if err := setPolicy.MarkFlagRequired("file"); err != nil { | ||
log.Fatal().Err(err).Msg("") | ||
} | ||
policyCmd.AddCommand(setPolicy) | ||
} | ||
|
||
var policyCmd = &cobra.Command{ | ||
Use: "policy", | ||
Short: "Manage the Headscale ACL Policy", | ||
} | ||
|
||
var getPolicy = &cobra.Command{ | ||
Use: "get", | ||
Short: "Print the current ACL Policy", | ||
Aliases: []string{"show", "view", "fetch"}, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
ctx, client, conn, cancel := getHeadscaleCLIClient() | ||
defer cancel() | ||
defer conn.Close() | ||
|
||
request := &v1.GetPolicyRequest{} | ||
|
||
response, err := client.GetPolicy(ctx, request) | ||
if err != nil { | ||
log.Fatal().Err(err).Msg("Failed to get the policy") | ||
|
||
return | ||
} | ||
|
||
// TODO(pallabpain): Maybe print this better? | ||
SuccessOutput("", response.GetPolicy(), "hujson") | ||
}, | ||
} | ||
|
||
var setPolicy = &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 HuJSON 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("file") | ||
|
||
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 | ||
} | ||
|
||
request := &v1.SetPolicyRequest{Policy: string(policyBytes)} | ||
|
||
ctx, client, conn, cancel := getHeadscaleCLIClient() | ||
defer cancel() | ||
defer conn.Close() | ||
|
||
if _, err := client.SetPolicy(ctx, request); err != nil { | ||
log.Fatal().Err(err).Msg("Failed to set ACL Policy") | ||
|
||
return | ||
} | ||
|
||
SuccessOutput(nil, "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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.