示例#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
}
示例#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)
		}
	}
}
示例#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)
		}
	}
}
示例#4
0
func deletePolicy(c *config.Config, policyName string) error {
	defer ctxCancelFunc.Unlock()
	ctxCancelFunc.Lock()

	if _, ok := ctxCancelFunc.m[policyName]; !ok {
		return errors.New("policy not found")
	}
	log.Printf("deleting the policy %s...", policyName)

	delete(ctxCancelFunc.m, policyName)
	defer c.Unlock()
	c.Lock()
	for i, q := range c.PolicyConfig {
		if q.Name == policyName {
			c.PolicyConfig = append(c.PolicyConfig[:i], c.PolicyConfig[i+1:]...)
		}
	}

	if err := c.Save(); err != nil {
		return err
	}
	return nil
}