2018-05-07 23:50:20 +00:00
package main
import (
2021-11-22 14:13:26 +00:00
"errors"
"fmt"
2022-08-05 22:07:32 +00:00
"os"
2023-02-15 18:01:44 +00:00
"path/filepath"
2024-07-09 13:14:25 +00:00
"strings"
2018-05-07 23:50:20 +00:00
2022-08-05 22:07:32 +00:00
"github.com/fleetdm/fleet/v4/pkg/spec"
2022-09-19 17:53:44 +00:00
"github.com/fleetdm/fleet/v4/server/fleet"
2021-03-13 00:42:38 +00:00
"github.com/urfave/cli/v2"
2018-05-07 23:50:20 +00:00
)
2021-03-13 00:42:38 +00:00
func applyCommand ( ) * cli . Command {
2022-09-19 17:53:44 +00:00
var (
flFilename string
flForce bool
flDryRun bool
)
2021-03-13 00:42:38 +00:00
return & cli . Command {
2018-05-07 23:50:20 +00:00
Name : "apply" ,
Usage : "Apply files to declaratively manage osquery configurations" ,
UsageText : ` fleetctl apply [options] ` ,
Flags : [ ] cli . Flag {
2021-03-13 00:42:38 +00:00
& cli . StringFlag {
2018-05-07 23:50:20 +00:00
Name : "f" ,
2021-03-13 00:42:38 +00:00
EnvVars : [ ] string { "FILENAME" } ,
2018-05-07 23:50:20 +00:00
Value : "" ,
Destination : & flFilename ,
Usage : "A file to apply" ,
} ,
2022-09-19 17:53:44 +00:00
& cli . BoolFlag {
Name : "force" ,
EnvVars : [ ] string { "FORCE" } ,
Destination : & flForce ,
Usage : "Force applying the file even if it raises validation errors (only supported for 'config' and 'team' specs)" ,
} ,
& cli . BoolFlag {
Name : "dry-run" ,
EnvVars : [ ] string { "DRY_RUN" } ,
Destination : & flDryRun ,
Usage : "Do not apply the file, just validate it (only supported for 'config' and 'team' specs)" ,
} ,
2023-02-22 16:14:53 +00:00
& cli . StringFlag {
Name : "policies-team" ,
Usage : "A team's name, this flag is only used on policies specs (overrides 'team' key in the policies file). This allows to easily import a group of policies to a team." ,
} ,
2021-02-03 02:55:16 +00:00
configFlag ( ) ,
contextFlag ( ) ,
debugFlag ( ) ,
2018-05-07 23:50:20 +00:00
} ,
Action : func ( c * cli . Context ) error {
if flFilename == "" {
return errors . New ( "-f must be specified" )
}
2022-08-05 22:07:32 +00:00
b , err := os . ReadFile ( flFilename )
2018-05-07 23:50:20 +00:00
if err != nil {
return err
}
2021-07-16 18:28:13 +00:00
fleetClient , err := clientFromCLI ( c )
2018-05-07 23:50:20 +00:00
if err != nil {
return err
}
2024-07-09 13:14:25 +00:00
// Check if the file has a .yml or .yaml extension
ext := strings . ToLower ( filepath . Ext ( flFilename ) )
if ext == "" {
return errors . New ( "Missing file extension: only .yml or .yaml files can be applied" )
}
if ext != ".yml" && ext != ".yaml" {
return fmt . Errorf ( "Invalid file extension %s: only .yml or .yaml files can be applied" , ext )
}
2022-08-05 22:07:32 +00:00
specs , err := spec . GroupFromBytes ( b )
if err != nil {
return err
}
logf := func ( format string , a ... interface { } ) {
fmt . Fprintf ( c . App . Writer , format , a ... )
}
2022-09-19 17:53:44 +00:00
2024-05-28 16:44:43 +00:00
opts := fleet . ApplyClientSpecOptions {
ApplySpecOptions : fleet . ApplySpecOptions {
Force : flForce ,
DryRun : flDryRun ,
} ,
2022-09-19 17:53:44 +00:00
}
2023-02-22 16:14:53 +00:00
if policiesTeamName := c . String ( "policies-team" ) ; policiesTeamName != "" {
opts . TeamForPolicies = policiesTeamName
}
2023-02-15 18:01:44 +00:00
baseDir := filepath . Dir ( flFilename )
2024-10-10 11:12:24 +00:00
teamsSoftwareInstallers := make ( map [ string ] [ ] fleet . SoftwarePackageResponse )
teamsScripts := make ( map [ string ] [ ] fleet . ScriptResponse )
2024-10-23 18:51:02 +00:00
_ , _ , _ , err = fleetClient . ApplyGroup ( c . Context , false , specs , baseDir , logf , nil , opts , teamsSoftwareInstallers , teamsScripts )
2018-05-07 23:50:20 +00:00
if err != nil {
return err
}
2022-01-28 19:28:07 +00:00
return nil
} ,
}
}