Exemplo n.º 1
0
func addSystemDataPolicy(c *config.Config) error {
	// if the policy already exists, return silently
	for _, p := range c.PolicyConfig {
		if p.Name == "default_system_data" {
			return nil
		}
	}

	p := policy.Policy{
		Name:     "default_system_data",
		AgentUID: c.UID,
		Type:     "system_data",
		M: map[string]string{
			"interval": "5s",
		},
	}
	if err := c.AddPolicy(p); err != nil {
		return err

	}
	if err := c.Save(); err != nil {
		return err
	}
	return nil
}
Exemplo n.º 2
0
func AddPolicyHandler(conf *config.Config) func(subj, reply string, p *policy.Policy) {
	return func(subj, reply string, p *policy.Policy) {
		log.Printf("add_policy received: %s\n", p.Name)
		if err := conf.AddPolicy(*p); err != nil {
			natsEncConn.Publish(reply, err.Error())
			return
		}
		if err := conf.Save(); err != nil {
			natsEncConn.Publish(reply, err.Error())
			return
		}
		ctx, cancel := context.WithCancel(context.Background())
		events, err := p.Execute(ctx)
		if err != nil {
			natsEncConn.Publish(reply, err.Error())
			return
		}
		ctxCancelFunc.Lock()
		ctxCancelFunc.m[p.Name] = cancel
		ctxCancelFunc.Unlock()

		natsEncConn.Publish(reply, "add_policy_ack") // acknowledge policy add
		for e := range events {
			natsEncConn.Publish("policy_events", e)
		}
	}
}
Exemplo n.º 3
0
func ModifyPolicyHandler(conf *config.Config) func(subj, reply string, p *policy.Policy) {
	return func(subj, reply string, p *policy.Policy) {
		log.Printf("modify_policy received: %s\n", p.Name)

		// We receive the complete policy with the new values
		// and delete the old policy and stop its execution.
		// Then we add the new policy.
		ctxCancelFunc.Lock()
		cancel := ctxCancelFunc.m[p.Name]
		ctxCancelFunc.Unlock()
		cancel()
		if err := deletePolicy(conf, p.Name); err != nil {
			log.Print(err)
			natsEncConn.Publish(reply, err.Error())
			return
		}
		log.Printf("adding the policy %s...", p.Name)
		if err := conf.AddPolicy(*p); err != nil {
			natsEncConn.Publish(reply, err.Error())
			return
		}
		if err := conf.Save(); err != nil {
			natsEncConn.Publish(reply, err.Error())
			return
		}
		ctx, cancel := context.WithCancel(context.Background())
		events, err := p.Execute(ctx)
		if err != nil {
			natsEncConn.Publish(reply, err.Error())
			return
		}
		ctxCancelFunc.Lock()
		ctxCancelFunc.m[p.Name] = cancel
		ctxCancelFunc.Unlock()

		natsEncConn.Publish(reply, "modify_policy_ack") // acknowledge policy delete
		for e := range events {
			natsEncConn.Publish("policy_events", e)
		}
	}
}