Skip to content

Commit 9528f6b

Browse files
Merge pull request #218 from censys/adh/virtual-host-update
feat(api): Search Virtual Hosts
2 parents 9a9d9cd + cd5d165 commit 9528f6b

File tree

14 files changed

+291
-138
lines changed

14 files changed

+291
-138
lines changed

.github/CODE_OF_CONDUCT.md

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
# Contributor Covenant Code of Conduct
32

43
## Our Pledge
@@ -18,23 +17,23 @@ diverse, inclusive, and healthy community.
1817
Examples of behavior that contributes to a positive environment for our
1918
community include:
2019

21-
* Demonstrating empathy and kindness toward other people
22-
* Being respectful of differing opinions, viewpoints, and experiences
23-
* Giving and gracefully accepting constructive feedback
24-
* Accepting responsibility and apologizing to those affected by our mistakes,
20+
- Demonstrating empathy and kindness toward other people
21+
- Being respectful of differing opinions, viewpoints, and experiences
22+
- Giving and gracefully accepting constructive feedback
23+
- Accepting responsibility and apologizing to those affected by our mistakes,
2524
and learning from the experience
26-
* Focusing on what is best not just for us as individuals, but for the
25+
- Focusing on what is best not just for us as individuals, but for the
2726
overall community
2827

2928
Examples of unacceptable behavior include:
3029

31-
* The use of sexualized language or imagery, and sexual attention or
30+
- The use of sexualized language or imagery, and sexual attention or
3231
advances of any kind
33-
* Trolling, insulting or derogatory comments, and personal or political attacks
34-
* Public or private harassment
35-
* Publishing others' private information, such as a physical or email
32+
- Trolling, insulting or derogatory comments, and personal or political attacks
33+
- Public or private harassment
34+
- Publishing others' private information, such as a physical or email
3635
address, without their explicit permission
37-
* Other conduct which could reasonably be considered inappropriate in a
36+
- Other conduct which could reasonably be considered inappropriate in a
3837
professional setting
3938

4039
## Enforcement Responsibilities
@@ -107,7 +106,7 @@ Violating these terms may lead to a permanent ban.
107106
### 4. Permanent Ban
108107

109108
**Community Impact**: Demonstrating a pattern of violation of community
110-
standards, including sustained inappropriate behavior, harassment of an
109+
standards, including sustained inappropriate behavior, harassment of an
111110
individual, or aggression toward or disparagement of classes of individuals.
112111

113112
**Consequence**: A permanent ban from any sort of public interaction within
@@ -119,15 +118,15 @@ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
119118
version 2.0, available at
120119
[https://www.contributor-covenant.org/version/2/0/code_of_conduct.html][v2.0].
121120

122-
Community Impact Guidelines were inspired by
123-
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
121+
Community Impact Guidelines were inspired by
122+
[Mozilla's code of conduct enforcement ladder][mozilla coc].
124123

125124
For answers to common questions about this code of conduct, see the FAQ at
126-
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available
125+
[https://www.contributor-covenant.org/faq][faq]. Translations are available
127126
at [https://www.contributor-covenant.org/translations][translations].
128127

129128
[homepage]: https://www.contributor-covenant.org
130129
[v2.0]: https://www.contributor-covenant.org/version/2/0/code_of_conduct.html
131-
[Mozilla CoC]: https://github.com/mozilla/diversity
132-
[FAQ]: https://www.contributor-covenant.org/faq
130+
[mozilla coc]: https://github.com/mozilla/diversity
131+
[faq]: https://www.contributor-covenant.org/faq
133132
[translations]: https://www.contributor-covenant.org/translations
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
---
22
name: Bug report
33
about: Create a report to help us improve
4-
title: ''
5-
labels: 'type: bug'
6-
assignees: ''
7-
4+
title: ""
5+
labels: "type: bug"
6+
assignees: ""
87
---
98

109
**Describe the bug**
1110
A clear and concise description of what the bug is.
1211

1312
**To Reproduce**
1413
Steps to reproduce the behavior:
14+
1515
1. Run '...'
1616
2. Then '....'
1717

1818
**Expected behavior**
1919
A clear and concise description of what you expected to happen.
2020

2121
**Device (please complete the following information):**
22-
- OS [e.g. macOS]:
23-
- Censys Python Version [e.g. 1.1.0]:
24-
- Shell [e.g. bash, zsh]:
22+
23+
- OS [e.g. debian, macOS]:
24+
- Censys Python Version [e.g. 2.1.0]: <!-- $ censys --version -->
25+
- Shell [e.g. bash, zsh]: <!-- $ echo $SHELL -->
2526

2627
**Additional context**
2728
Add any other context about the problem here.

.github/ISSUE_TEMPLATE/feature_request.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
---
22
name: Feature request
33
about: Suggest an idea for this project
4-
title: ''
5-
labels: 'type: feature request'
6-
assignees: ''
7-
4+
title: ""
5+
labels: "type: feature request"
6+
assignees: ""
87
---
98

109
**Is your feature request related to a problem? Please describe.**

.github/ISSUE_TEMPLATE/question.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
---
22
name: Question
33
about: Ask a question about this project
4-
title: ''
5-
labels: 'type: question'
6-
assignees: ''
7-
4+
title: ""
5+
labels: "type: question"
6+
assignees: ""
87
---
98

109
**What is your question?**
11-
A clear and concise question. Ex. What can I use ___ for? [...]
10+
A clear and concise question. Ex. What can I use \_\_\_ for? [...]

.github/PULL_REQUEST_TEMPLATE/pull_request_template.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Description
22

33
<!--
4-
Please include a summary of the change(s) and which issue(s) is fixed.
5-
Please also include relevant motivation and context.
4+
Please include a summary of the change(s) and which issue(s) is fixed.
5+
Please also include relevant motivation and context.
66
List any dependencies that are required for this change.
77
-->
88

.github/workflows/stale.yml

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,21 @@ name: Mark stale issues and pull requests
88
on:
99
workflow_dispatch:
1010
schedule:
11-
- cron: '30 12 * * *'
11+
- cron: "30 12 * * *"
1212

1313
jobs:
1414
stale:
15-
1615
runs-on: ubuntu-latest
1716
timeout-minutes: 5
1817
permissions:
1918
issues: write
2019
pull-requests: write
2120

2221
steps:
23-
- uses: actions/stale@v3
24-
with:
25-
repo-token: ${{ secrets.GITHUB_TOKEN }}
26-
days-before-stale: 60
27-
days-before-close: 7
28-
stale-issue-label: 'stale'
29-
stale-pr-label: 'stale'
22+
- uses: actions/stale@v3
23+
with:
24+
repo-token: ${{ secrets.GITHUB_TOKEN }}
25+
days-before-stale: 60
26+
days-before-close: 7
27+
stale-issue-label: "stale"
28+
stale-pr-label: "stale"

censys/cli/commands/search.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ def cli_search(args: argparse.Namespace):
101101
if args.pages:
102102
search_args["pages"] = args.pages
103103

104+
if args.virtual_hosts:
105+
search_args["virtual_hosts"] = args.virtual_hosts
106+
104107
with err_console.status("Searching"):
105108
query = index.search(args.query, **search_args)
106109

@@ -183,6 +186,14 @@ def include(parent_parser: argparse._SubParsersAction, parents: dict):
183186
type=int,
184187
help="number of pages of results to return (when set to -1 returns all pages available)",
185188
)
189+
v2_group.add_argument(
190+
"--virtual-hosts",
191+
type=str,
192+
default="EXCLUDE",
193+
choices=["INCLUDE", "EXCLUDE", "ONLY"],
194+
metavar="INCLUDE|EXCLUDE|ONLY",
195+
help="whether to include virtual hosts in the results",
196+
)
186197

187198
v1_group = search_parser.add_argument_group(
188199
f"v1 specific arguments ({', '.join(V1_INDEXES)})"

censys/search/v2/api.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ def __init__(
111111
per_page: Optional[int] = None,
112112
cursor: Optional[str] = None,
113113
pages: int = 1,
114+
**kwargs: Any,
114115
):
115116
"""Inits Query.
116117
@@ -120,6 +121,7 @@ def __init__(
120121
per_page (int): Optional; The number of results to be returned for each page. Defaults to 100.
121122
cursor (int): Optional; The cursor of the desired result set.
122123
pages (int): Optional; The number of pages returned. Defaults to 1. If you set this to -1, it will return all pages.
124+
**kwargs (Any): Optional; Additional arguments to be passed to the query.
123125
"""
124126
self.api = api
125127
self.query = query
@@ -131,6 +133,7 @@ def __init__(
131133
self.pages = float("inf")
132134
else:
133135
self.pages = pages
136+
self.extra_args = kwargs
134137

135138
def __call__(self, per_page: Optional[int] = None) -> List[dict]:
136139
"""Search current index.
@@ -151,6 +154,7 @@ def __call__(self, per_page: Optional[int] = None) -> List[dict]:
151154
"q": self.query,
152155
"per_page": per_page or self.per_page or 100,
153156
"cursor": self.nextCursor or self.cursor,
157+
**self.extra_args,
154158
}
155159
payload = self.api._get(self.api.search_path, args)
156160
self.page += 1
@@ -209,6 +213,7 @@ def search(
209213
per_page: Optional[int] = None,
210214
cursor: Optional[str] = None,
211215
pages: int = 1,
216+
**kwargs: Any,
212217
) -> Query:
213218
"""Search current index.
214219
@@ -220,11 +225,12 @@ def search(
220225
per_page (int): Optional; The number of results to be returned for each page. Defaults to 100.
221226
cursor (int): Optional; The cursor of the desired result set.
222227
pages (int): Optional; The number of pages returned. Defaults to 1.
228+
**kwargs (Any): Optional; Additional arguments to be passed to the query.
223229
224230
Returns:
225231
Query: Query object that can be a callable or an iterable.
226232
"""
227-
return self.Query(self, query, per_page, cursor, pages)
233+
return self.Query(self, query, per_page, cursor, pages, **kwargs)
228234

229235
def view(
230236
self,

censys/search/v2/hosts.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""Interact with the Censys Search Host API."""
2-
from typing import List, Optional
2+
from typing import Any, List, Optional
33

44
from .api import CensysSearchAPIv2
55
from censys.common.types import Datetime
@@ -72,6 +72,35 @@ class CensysHosts(CensysSearchAPIv2):
7272
INDEX_NAME = "hosts"
7373
"""Name of Censys Index."""
7474

75+
def search(
76+
self,
77+
query: str,
78+
per_page: Optional[int] = None,
79+
cursor: Optional[str] = None,
80+
pages: int = 1,
81+
virtual_hosts: Optional[str] = None,
82+
**kwargs: Any,
83+
) -> CensysSearchAPIv2.Query:
84+
"""Search host index.
85+
86+
Searches the given index for all records that match the given query.
87+
For more details, see our documentation: https://search.censys.io/api
88+
89+
Args:
90+
query (str): The query to be executed.
91+
per_page (int): Optional; The number of results to be returned for each page. Defaults to 100.
92+
cursor (int): Optional; The cursor of the desired result set.
93+
virtual_hosts (str): Optional; Whether to include virtual hosts in the results. Valid values are "EXCLUDE", "INCLUDE", and "ONLY".
94+
pages (int): Optional; The number of pages returned. Defaults to 1.
95+
**kwargs (Any): Optional; Additional arguments to be passed to the query.
96+
97+
Returns:
98+
Query: Query object that can be a callable or an iterable.
99+
"""
100+
if virtual_hosts:
101+
kwargs["virtual_hosts"] = virtual_hosts
102+
return super().search(query, per_page, cursor, pages, **kwargs)
103+
75104
def view_host_names(self, ip_address: str) -> List[str]:
76105
"""Fetches a list of host names for the specified IP address.
77106

examples/search/search_hosts.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,9 @@
1515
# View all results
1616
query = c.v2.hosts.search("service.service_name: HTTP", per_page=5, pages=2)
1717
print(query.view_all())
18+
19+
# Search for virtual hosts
20+
query = c.v2.hosts.search(
21+
"NOT services.service_name: HTTP", per_page=5, virtual_hosts="ONLY"
22+
)
23+
print(query())

0 commit comments

Comments
 (0)