-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
363 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,6 +59,7 @@ func TestLoad(t *testing.T) { | |
To: "[email protected]", | ||
APIKey: "hoge", | ||
}, | ||
FilterConfig: FilterConfig{Filters: []string{}}, | ||
}, | ||
}, | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,8 +13,11 @@ certspotter_token = "dummy" | |
[alert_config] | ||
mailer_config = "sendgrid" | ||
|
||
[position_config] | ||
filename = "positions.toml" | ||
[position_config] | ||
filename = "positions.toml" | ||
|
||
[filter_config] | ||
filters = [] | ||
|
||
[smtp] | ||
from = "[email protected]" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package filter | ||
|
||
import ( | ||
"github.com/Hsn723/certspotter-client/api" | ||
"github.com/hashicorp/go-plugin" | ||
"net/rpc" | ||
) | ||
|
||
var ( | ||
HandshakeConfig = plugin.HandshakeConfig{ | ||
ProtocolVersion: 1, | ||
MagicCookieKey: "CT_MONITOR_PLUGIN", | ||
MagicCookieValue: "issuance_filter", | ||
} | ||
PluginKey = "issuance" | ||
PluginMap = map[string]plugin.Plugin{ | ||
PluginKey: &IssuanceFilterPlugin{}, | ||
} | ||
) | ||
|
||
// IssuanceFilter is the interface exposed as a plugin. | ||
type IssuanceFilter interface { | ||
Filter(is []api.Issuance) ([]api.Issuance, error) | ||
} | ||
|
||
// IssuanceFilterRPC is a plugin implementation over RPC. | ||
type IssuanceFilterRPCClient struct { | ||
client *rpc.Client | ||
} | ||
|
||
func (f *IssuanceFilterRPCClient) Filter(is []api.Issuance) ([]api.Issuance, error) { | ||
var resp []api.Issuance | ||
err := f.client.Call("Plugin.Filter", is, &resp) | ||
return resp, err | ||
} | ||
|
||
// IssuanceFilterRPCServer is the RPC server that IssuanceFilterRPC talks to. | ||
type IssuanceFilterRPCServer struct { | ||
Impl IssuanceFilter | ||
} | ||
|
||
func (s *IssuanceFilterRPCServer) Filter(is []api.Issuance, resp *[]api.Issuance) error { | ||
r, err := s.Impl.Filter(is) | ||
*resp = r | ||
return err | ||
} | ||
|
||
// IssuanceFilterPlugin us an implementation of plugin.Plugin. | ||
type IssuanceFilterPlugin struct { | ||
Impl IssuanceFilter | ||
} | ||
|
||
func (p *IssuanceFilterPlugin) Server(*plugin.MuxBroker) (interface{}, error) { | ||
return &IssuanceFilterRPCServer{Impl: p.Impl}, nil | ||
} | ||
|
||
func (*IssuanceFilterPlugin) Client(b *plugin.MuxBroker, c *rpc.Client) (interface{}, error) { | ||
return &IssuanceFilterRPCClient{client: c}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package filter | ||
|
||
import ( | ||
"os/exec" | ||
"time" | ||
|
||
"github.com/Hsn723/certspotter-client/api" | ||
"github.com/hashicorp/go-plugin" | ||
) | ||
|
||
// ApplyFilters runs the filter plugins and returns the resulting issuances. | ||
// Plugin errors are ignored. It is up to the plugin to log them appropriately. | ||
func ApplyFilters(filterPaths []string, issuances []api.Issuance) ([]api.Issuance, error) { | ||
res := issuances | ||
for _, fp := range filterPaths { | ||
r, err := applyFilter(fp, res) | ||
if err != nil { | ||
return res, err | ||
} | ||
res = r | ||
} | ||
return res, nil | ||
} | ||
|
||
func applyFilter(filter string, issuances []api.Issuance) ([]api.Issuance, error) { | ||
client := plugin.NewClient(&plugin.ClientConfig{ | ||
HandshakeConfig: HandshakeConfig, | ||
Plugins: PluginMap, | ||
Cmd: exec.Command(filter), | ||
AllowedProtocols: []plugin.Protocol{plugin.ProtocolNetRPC}, | ||
StartTimeout: 10 * time.Second, | ||
Managed: true, | ||
}) | ||
defer client.Kill() | ||
|
||
rpcClient, err := client.Client() | ||
if err != nil { | ||
return issuances, err | ||
} | ||
|
||
raw, err := rpcClient.Dispense(PluginKey) | ||
if err != nil { | ||
return issuances, err | ||
} | ||
|
||
issuanceFilter := raw.(IssuanceFilter) | ||
return issuanceFilter.Filter(issuances) | ||
} |
Oops, something went wrong.