Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions packages/cmd/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ var gatewaySystemdCmd = &cobra.Command{
Short: "Manage systemd service for Infisical gateway",
Long: "Manage systemd service for Infisical gateway. Use 'systemd install' to install and enable the service.",
Example: `sudo infisical gateway systemd install my-gateway --token=<token> --domain=<domain>
sudo infisical gateway systemd uninstall`,
sudo infisical gateway systemd uninstall my-gateway`,
DisableFlagsInUseLine: true,
Args: cobra.NoArgs,
}
Expand Down Expand Up @@ -619,6 +619,8 @@ var gatewaySystemdInstallCmd = &cobra.Command{

enrollMethod, _ := cmd.Flags().GetString("enroll-method")

var installedServiceName string

if enrollMethod == gatewayv2.EnrollMethodToken {
// --- Enrollment token path ---
enrollToken, flagErr := cmd.Flags().GetString("token")
Expand All @@ -642,9 +644,11 @@ var gatewaySystemdInstallCmd = &cobra.Command{
}

// Install systemd service using the long-lived access token
if installErr := gatewayv2.InstallEnrolledGatewaySystemdService(enrollResp.AccessToken, domain, gatewayName, relayName, serviceLogFile); installErr != nil {
svcName, installErr := gatewayv2.InstallEnrolledGatewaySystemdService(enrollResp.AccessToken, domain, gatewayName, relayName, serviceLogFile)
if installErr != nil {
util.HandleError(installErr, "Unable to install systemd service")
}
installedServiceName = svcName
} else if enrollMethod == gatewayv2.EnrollMethodAws {
// --- AWS Auth path ---
// Don't perform the AWS login at install time — the gateway does it on each service
Expand All @@ -656,9 +660,11 @@ var gatewaySystemdInstallCmd = &cobra.Command{

relayName, _ := util.GetRelayName(cmd, false, "")

if installErr := gatewayv2.InstallAwsAuthGatewaySystemdService(gatewayID, domain, gatewayName, relayName, serviceLogFile); installErr != nil {
svcName, installErr := gatewayv2.InstallAwsAuthGatewaySystemdService(gatewayID, domain, gatewayName, relayName, serviceLogFile)
if installErr != nil {
util.HandleError(installErr, "Unable to install systemd service")
}
installedServiceName = svcName
} else {
// --- Machine identity token path ---
token, tokenErr := util.GetInfisicalToken(cmd)
Expand All @@ -675,38 +681,44 @@ var gatewaySystemdInstallCmd = &cobra.Command{
util.HandleError(relayErr, "unable to get relay name")
}

if installErr := gatewayv2.InstallGatewaySystemdService(token.Token, domain, gatewayName, relayName, serviceLogFile); installErr != nil {
svcName, installErr := gatewayv2.InstallGatewaySystemdService(token.Token, domain, gatewayName, relayName, serviceLogFile)
if installErr != nil {
util.HandleError(installErr, "Unable to install systemd service")
}
installedServiceName = svcName
}

enableCmd := exec.Command("systemctl", "enable", "infisical-gateway")
if installedServiceName == "" {
return
}

enableCmd := exec.Command("systemctl", "enable", installedServiceName)
if err := enableCmd.Run(); err != nil {
util.HandleError(err, "Failed to enable systemd service")
}

log.Info().Msg("Successfully installed and enabled infisical-gateway service")
log.Info().Msg("To start the service, run: sudo systemctl start infisical-gateway")
log.Info().Msgf("Successfully installed and enabled %s service", installedServiceName)
log.Info().Msgf("To start the service, run: sudo systemctl start %s", installedServiceName)
},
}

var gatewaySystemdUninstallCmd = &cobra.Command{
Use: "uninstall",
Use: "uninstall [name]",
Short: "Uninstall and remove systemd service for the gateway (requires sudo)",
Long: "Uninstall and remove systemd service for the gateway. Must be run with sudo on Linux.",
Example: "sudo infisical gateway systemd uninstall",
Example: "sudo infisical gateway systemd uninstall my-gateway",
DisableFlagsInUseLine: true,
Args: cobra.NoArgs,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if runtime.GOOS != "linux" {
util.HandleError(fmt.Errorf("systemd service installation is only supported on Linux"))
util.HandleError(fmt.Errorf("systemd service uninstallation is only supported on Linux"))
}

if os.Geteuid() != 0 {
util.HandleError(fmt.Errorf("systemd service installation requires root/sudo privileges"))
util.HandleError(fmt.Errorf("systemd service uninstallation requires root/sudo privileges"))
}

if err := gatewayv2.UninstallGatewaySystemdService(); err != nil {
if err := gatewayv2.UninstallGatewaySystemdService(args[0]); err != nil {
util.HandleError(err, "Failed to uninstall systemd service")
}
},
Expand Down
22 changes: 13 additions & 9 deletions packages/gateway-v2/enroll.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,14 @@ func gatewayConfPath(name string) (string, error) {
return filepath.Join(homeDir, ".infisical", "gateways", name+".conf"), nil
}

// loadConfKey reads a key from the named gateway's config file. Returns empty string if not found.
func loadConfKey(name, key string) (string, error) {
confPath, err := gatewayConfPath(name)
if err != nil {
return "", err
}

data, err := os.ReadFile(confPath)
// readKeyFromConfFile reads a key=value pair from a config file at the given path.
func readKeyFromConfFile(path, key string) (string, error) {
data, err := os.ReadFile(path)
if os.IsNotExist(err) {
return "", nil
}
if err != nil {
return "", fmt.Errorf("failed to read gateway config: %w", err)
return "", fmt.Errorf("failed to read config file: %w", err)
}

prefix := key + "="
Expand All @@ -56,6 +51,15 @@ func loadConfKey(name, key string) (string, error) {
return "", nil
}

// loadConfKey reads a key from the named gateway's config file. Returns empty string if not found.
func loadConfKey(name, key string) (string, error) {
confPath, err := gatewayConfPath(name)
if err != nil {
return "", err
}
return readKeyFromConfFile(confPath, key)
}

// saveConfKey writes a key=value pair to the named gateway's config file, preserving other keys.
// The file is created with 0600 permissions (owner read/write only).
func saveConfKey(name, key, value string) error {
Expand Down
Loading
Loading