forked from pivotal-cf/cred-alert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_server.go
98 lines (80 loc) · 2.26 KB
/
search_server.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
package api
import (
"code.cloudfoundry.org/lager"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"red/redpb"
"cred-alert/revokpb"
"cred-alert/search"
"cred-alert/sniff/matchers"
)
type SearchServer struct {
logger lager.Logger
searcher search.Searcher
blobSearcher search.BlobSearcher
}
func NewSearchServer(
logger lager.Logger,
searcher search.Searcher,
blobSearcher search.BlobSearcher,
) *SearchServer {
return &SearchServer{
logger: logger,
searcher: searcher,
blobSearcher: blobSearcher,
}
}
func (s *SearchServer) BoshBlobs(ctx context.Context, request *revokpb.BoshBlobsRequest) (*revokpb.BoshBlobsResponse, error) {
logger := s.logger.Session("bosh-blobs-endpoint")
logger.Info("intiating-blob-search")
repository := request.GetRepository()
blobs, err := s.blobSearcher.ListBlobs(logger, repository.GetOwner(), repository.GetName())
if err != nil {
return nil, err
}
response := revokpb.BoshBlobsResponse{}
for _, blob := range blobs {
response.Blobs = append(response.Blobs, &revokpb.BoshBlob{
Path: blob.Path,
Sha: blob.SHA,
})
}
return &response, nil
}
func (s *SearchServer) Search(query *revokpb.SearchQuery, stream revokpb.RevokSearch_SearchServer) error {
logger := s.logger.Session("search-endpoint")
logger.Info("intiating-search")
regex := query.GetRegex()
if regex == "" {
return grpc.Errorf(codes.InvalidArgument, "query regular expression may not be empty")
}
matcher, err := matchers.TryFormat(regex)
if err != nil {
return grpc.Errorf(codes.InvalidArgument, "query regular expression is invalid: '%s'", regex)
}
results := s.searcher.SearchCurrent(stream.Context(), logger, matcher)
for result := range results.C() {
searchResult := &revokpb.SearchResult{
Location: &revokpb.SourceLocation{
Repository: &redpb.Repository{
Owner: result.Owner,
Name: result.Repository,
},
Revision: result.Revision,
Path: result.Path,
LineNumber: uint32(result.LineNumber),
Location: uint32(result.Location),
Length: uint32(result.Length),
},
Content: result.Content,
}
if err := stream.Send(searchResult); err != nil {
return err
}
}
if err := results.Err(); err != nil {
return err
}
return nil
}