forked from intelsdi-x/snap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
150 lines (124 loc) · 2.81 KB
/
plugin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
http://www.apache.org/licenses/LICENSE-2.0.txt
Copyright 2015 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package core
import (
"crypto/sha256"
"fmt"
"io/ioutil"
"time"
"github.com/intelsdi-x/snap/core/cdata"
)
type Plugin interface {
TypeName() string
Name() string
Version() int
}
type PluginType int
func ToPluginType(name string) (PluginType, error) {
pts := map[string]PluginType{
"collector": 0,
"processor": 1,
"publisher": 2,
}
t, ok := pts[name]
if !ok {
return -1, fmt.Errorf("invalid plugin type name given %s", name)
}
return t, nil
}
func (pt PluginType) String() string {
return []string{
"collector",
"processor",
"publisher",
}[pt]
}
const (
// List of plugin type
CollectorPluginType PluginType = iota
ProcessorPluginType
PublisherPluginType
)
type AvailablePlugin interface {
Plugin
HitCount() int
LastHit() time.Time
ID() uint32
}
// the public interface for a plugin
// this should be the contract for
// how mgmt modules know a plugin
type CatalogedPlugin interface {
Plugin
IsSigned() bool
Status() string
PluginPath() string
LoadedTimestamp() *time.Time
}
// the collection of cataloged plugins used
// by mgmt modules
type PluginCatalog []CatalogedPlugin
type SubscribedPlugin interface {
Plugin
Config() *cdata.ConfigDataNode
}
type RequestedPlugin struct {
path string
checkSum [sha256.Size]byte
signature []byte
}
func NewRequestedPlugin(path string) (*RequestedPlugin, error) {
rp := &RequestedPlugin{
path: path,
signature: nil,
}
err := rp.generateCheckSum()
if err != nil {
return nil, err
}
return rp, nil
}
func (p *RequestedPlugin) Path() string {
return p.path
}
func (p *RequestedPlugin) CheckSum() [sha256.Size]byte {
return p.checkSum
}
func (p *RequestedPlugin) Signature() []byte {
return p.signature
}
func (p *RequestedPlugin) SetPath(path string) {
p.path = path
}
func (p *RequestedPlugin) SetSignature(data []byte) {
p.signature = data
}
func (p *RequestedPlugin) generateCheckSum() error {
var b []byte
var err error
if b, err = ioutil.ReadFile(p.path); err != nil {
return err
}
p.checkSum = sha256.Sum256(b)
return nil
}
func (p *RequestedPlugin) ReadSignatureFile(file string) error {
var b []byte
var err error
if b, err = ioutil.ReadFile(file); err != nil {
return err
}
p.SetSignature(b)
return nil
}