-
Notifications
You must be signed in to change notification settings - Fork 590
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add cli for simple gateway registration
Signed-off-by: Christine Wang <christinewang@fb.com>
- Loading branch information
1 parent
92f7871
commit af8a169
Showing
4 changed files
with
155 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
#!/usr/bin/env python3 | ||
""" | ||
Copyright 2020 The Magma Authors. | ||
This source code is licensed under the BSD-style license found in the | ||
LICENSE file in the root directory of this source tree. | ||
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. | ||
""" | ||
|
||
import argparse | ||
import sys | ||
import textwrap | ||
from typing import List | ||
|
||
import snowflake | ||
from magma.common.cert_utils import load_public_key_to_base64der | ||
from magma.common.service_registry import ServiceRegistry | ||
from magma.configuration.service_configs import save_override_config | ||
from orc8r.protos.bootstrapper_pb2 import ( | ||
ChallengeKey, | ||
RegisterRequest, | ||
RegisterResponse, | ||
) | ||
from orc8r.protos.bootstrapper_pb2_grpc import RegistrationStub | ||
from orc8r.protos.identity_pb2 import AccessGatewayID | ||
|
||
|
||
def register_handler(client: RegistrationStub, args: List[str]) -> RegisterResponse: | ||
""" | ||
Registers a device and retrieves its control proxy | ||
Returns: | ||
RegisterResponse: response from gRPC call, either error or the control_proxy | ||
""" | ||
req = RegisterRequest( | ||
token=args.token, | ||
hwid=AccessGatewayID( | ||
id=snowflake.snowflake(), | ||
), | ||
challenge_key=ChallengeKey( | ||
key=load_public_key_to_base64der("/var/opt/magma/certs/gw_challenge.key"), | ||
), | ||
) | ||
|
||
res = client.Register(req) | ||
if res.HasField("error"): | ||
raise Exception(res.error) | ||
|
||
msg = textwrap.dedent( | ||
""" | ||
> Successfully registered gateway | ||
Hardware ID | ||
----------- | ||
{} | ||
Challenge Key | ||
----------- | ||
{} | ||
Control Proxy | ||
----------- | ||
{} | ||
""", | ||
) | ||
print(msg.format(req.hwid, req.challenge_key, res.control_proxy)) | ||
|
||
return res | ||
|
||
|
||
def main(): | ||
"""Register a gateway""" | ||
parser = argparse.ArgumentParser(description="Register a gateway.") | ||
parser.add_argument( | ||
"domain", | ||
metavar="DOMAIN_NAME", | ||
type=str, | ||
help="orc8r's domain name", | ||
) | ||
parser.add_argument( | ||
"token", | ||
metavar="REGISTRATION_TOKEN", | ||
type=str, | ||
help="registration token after API call", | ||
) | ||
parser.add_argument( | ||
"--ca-file", | ||
type=str, | ||
help="orc8r's root CA file", | ||
) | ||
parser.add_argument( | ||
"--cloud-port", | ||
type=str, | ||
help="orc8r's port", | ||
) | ||
parser.add_argument( | ||
"--no_control_proxy", | ||
action="store_true", | ||
help="disables writing the control proxy file", | ||
) | ||
args = parser.parse_args() | ||
chan = ServiceRegistry.get_bare_bootstrap_rpc_channel( | ||
args.domain, | ||
'8444' if not args.cloud_port else args.cloud_port, | ||
args.ca_file, | ||
) | ||
client = RegistrationStub(chan) | ||
try: | ||
res = register_handler(client, args) | ||
except Exception as e: | ||
msg = textwrap.dedent( | ||
""" | ||
> Error: {} | ||
""", | ||
) | ||
print(msg.format(e)) | ||
sys.exit(1) | ||
|
||
if not args.no_control_proxy: | ||
save_override_config("control_proxy", res.control_proxy) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |