-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrdap-nameserver.go
207 lines (172 loc) · 5.52 KB
/
rdap-nameserver.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package adapters
import (
"context"
"fmt"
"strings"
"github.com/openrdap/rdap"
"github.com/overmindtech/cli/sdp-go"
"github.com/overmindtech/cli/sdpcache"
)
type RdapNameserverAdapter struct {
ClientFac func() *rdap.Client
Cache *sdpcache.Cache
}
// Type is the type of items that this returns
func (s *RdapNameserverAdapter) Type() string {
return "rdap-nameserver"
}
// Name Returns the name of the adapter
func (s *RdapNameserverAdapter) Name() string {
return "rdap"
}
// Weighting of duplicate adapters
func (s *RdapNameserverAdapter) Weight() int {
return 100
}
func (s *RdapNameserverAdapter) Metadata() *sdp.AdapterMetadata {
return rdapNameserverMetadata
}
var rdapNameserverMetadata = Metadata.Register(&sdp.AdapterMetadata{
DescriptiveName: "RDAP Nameserver",
Type: "rdap-nameserver",
SupportedQueryMethods: &sdp.AdapterSupportedQueryMethods{
Search: true,
SearchDescription: "Search for the RDAP entry for a nameserver by its full URL e.g. \"https://rdap.verisign.com/com/v1/nameserver/NS4.GOOGLE.COM\"",
},
PotentialLinks: []string{"dns", "ip", "rdap-entity"},
Category: sdp.AdapterCategory_ADAPTER_CATEGORY_NETWORK,
})
func (s *RdapNameserverAdapter) Scopes() []string {
return []string{
"global",
}
}
func (s *RdapNameserverAdapter) Get(ctx context.Context, scope string, query string, ignoreCache bool) (*sdp.Item, error) {
// Check the cache for GET requests, if we don't hit the cache then there is
// nothing we can do though
hit, _, items, sdpErr := s.Cache.Lookup(ctx, s.Name(), sdp.QueryMethod_GET, scope, s.Type(), query, ignoreCache)
if sdpErr != nil {
return nil, sdpErr
}
if hit {
if len(items) > 0 {
return items[0], nil
}
}
// This adapter doesn't technically support the GET method (since you can't
// use the handle to query for an IP network)
return nil, &sdp.QueryError{
ErrorType: sdp.QueryError_NOTFOUND,
Scope: scope,
ErrorString: fmt.Sprintf("Nameservers can't be queried by handle, use the SEARCH method instead"),
}
}
func (s *RdapNameserverAdapter) List(ctx context.Context, scope string, ignoreCache bool) ([]*sdp.Item, error) {
return nil, &sdp.QueryError{
ErrorType: sdp.QueryError_NOTFOUND,
Scope: scope,
ErrorString: fmt.Sprintf("Nameservers cannot be listed, use the SEARCH method instead"),
}
}
// Search for the nameserver using the full RDAP URL. This is required since
// nameserver queries are not capable of being bootstrapped and we need to know
// which nameserver to query from the beginning. Fortunately domain queries can
// be bootstrapped, so we can use the domain query to find the nameserver in the
// link
func (s *RdapNameserverAdapter) Search(ctx context.Context, scope string, query string, ignoreCache bool) ([]*sdp.Item, error) {
hit, ck, items, sdpErr := s.Cache.Lookup(ctx, s.Name(), sdp.QueryMethod_SEARCH, scope, s.Type(), query, ignoreCache)
if sdpErr != nil {
return nil, sdpErr
}
if hit {
return items, nil
}
parsed, err := parseRdapUrl(query)
if err != nil {
return nil, err
}
request := &rdap.Request{
Type: rdap.NameserverRequest,
Query: parsed.Query,
Server: parsed.ServerRoot,
}
request.WithContext(ctx)
response, err := s.ClientFac().Do(request)
if err != nil {
err = wrapRdapError(err)
s.Cache.StoreError(err, RdapCacheDuration, ck)
return nil, err
}
if response.Object == nil {
return nil, &sdp.QueryError{
ErrorType: sdp.QueryError_NOTFOUND,
Scope: scope,
ErrorString: fmt.Sprintf("No IP Network found for %s", query),
SourceName: s.Name(),
}
}
nameserver, ok := response.Object.(*rdap.Nameserver)
if !ok {
return nil, fmt.Errorf("Expected Nameserver, got %T", response.Object)
}
attributes, err := sdp.ToAttributesCustom(map[string]interface{}{
"conformance": nameserver.Conformance,
"objectClassName": nameserver.ObjectClassName,
"notices": nameserver.Notices,
"handle": nameserver.Handle,
"ldhName": nameserver.LDHName,
"unicodeName": nameserver.UnicodeName,
"ipAddresses": nameserver.IPAddresses,
"status": nameserver.Status,
"remarks": nameserver.Remarks,
"links": nameserver.Links,
"port43": nameserver.Port43,
"events": nameserver.Events,
}, true, RDAPTransforms)
if err != nil {
return nil, err
}
item := &sdp.Item{
Type: s.Type(),
UniqueAttribute: "ldhName",
Attributes: attributes,
Scope: scope,
}
// Link entities
item.LinkedItemQueries = append(item.LinkedItemQueries, extractEntityLinks(nameserver.Entities)...)
// Nameservers are resolvable in DNS too
item.LinkedItemQueries = append(item.LinkedItemQueries, &sdp.LinkedItemQuery{
Query: &sdp.Query{
Type: "dns",
Method: sdp.QueryMethod_SEARCH,
Query: strings.ToLower(nameserver.LDHName),
Scope: "global",
},
BlastPropagation: &sdp.BlastPropagation{
// These represent the same thing so linked them both ways
In: true,
Out: true,
},
})
// Link IP addresses
if nameserver.IPAddresses != nil {
allIPs := append(nameserver.IPAddresses.V4, nameserver.IPAddresses.V6...)
for _, ip := range allIPs {
item.LinkedItemQueries = append(item.LinkedItemQueries, &sdp.LinkedItemQuery{
Query: &sdp.Query{
Type: "ip",
Method: sdp.QueryMethod_GET,
Query: ip,
Scope: "global",
},
BlastPropagation: &sdp.BlastPropagation{
// IPs are always linked
In: true,
Out: true,
},
})
}
}
s.Cache.StoreItem(item, RdapCacheDuration, ck)
return []*sdp.Item{item}, nil
}