-
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
42eebde
commit c0b4922
Showing
8 changed files
with
170 additions
and
22 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
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,124 @@ | ||
#!/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 asyncio | ||
import os | ||
|
||
import grpc | ||
import snowflake | ||
|
||
from orc8r.protos.identity_pb2 import AccessGatewayID, Identity | ||
from orc8r.protos.bootstrapper_pb2_grpc import BootstrapperStub | ||
from magma.common.cert_utils import load_public_key_to_base64der | ||
from orc8r.protos.bootstrapper_pb2 import ( | ||
RegisterRequest, | ||
GetGatewayDeviceInfoRequest, | ||
) | ||
from orc8r.protos.bootstrapper_pb2_grpc import ( | ||
RegistrationStub, # Register | ||
CloudRegistrationStub, # Register | ||
) | ||
from magma.common.service_registry import ServiceRegistry | ||
|
||
def bare_grpc_wrapper(func): | ||
""" | ||
Wraps a function with a gRPC wrapper which creates a RPC client to | ||
the service and handles any RPC Exceptions. | ||
Usage: | ||
@cloud_grpc_wrapper | ||
def func(client, args): | ||
pass | ||
func(args, ProtoStubClass, 'service') | ||
""" | ||
|
||
def wrapper(*alist): | ||
args = alist[0] | ||
stub_cls = alist[1] | ||
chan = ServiceRegistry.get_bare_rpc_channel() | ||
client = stub_cls(chan) | ||
try: | ||
func(client, args) | ||
except Exception as err: | ||
print(err) | ||
exit(1) | ||
|
||
return wrapper | ||
|
||
|
||
@bare_grpc_wrapper | ||
def register_handler(client, args): | ||
req = RegisterRequest() | ||
req.token = args.token | ||
req.hwid.id = snowflake.snowflake() | ||
req.challenge_key.key = load_public_key_to_base64der( | ||
"/var/opt/magma/certs/gw_challenge.key") | ||
req.challenge_key.key_type = 0 | ||
|
||
res = client.Register(req) | ||
if res.HasField("error"): | ||
raise Exception(res.error) | ||
|
||
print("Successfully registered gateway") | ||
print("Hardware ID\n-----------\n%s" % req.hwid) | ||
print("Challenge Key\n-----------\n%s\n" % req.challenge_key) | ||
print("Control Proxy\n-----------\n%s\n\n" % res.control_proxy) | ||
return res | ||
|
||
|
||
@bare_grpc_wrapper | ||
def get_gateway_device_info_handler(client, args): | ||
device_info_request = GetGatewayDeviceInfoRequest() | ||
device_info_request.token = args.token | ||
|
||
res = client.GetGatewayDeviceInfo(device_info_request) | ||
if res.HasField("error"): | ||
raise grpc.RpcError(res) | ||
|
||
print("Successfully retrieved gateway device info") | ||
print("Network ID\n-----------\n%s" % res.gateway_device_info.network_id) | ||
print("Logical ID\n-----------\n%s\n\n" % res.gateway_device_info.logical_id) | ||
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" | ||
) | ||
args = parser.parse_args() | ||
|
||
register_handler(args, RegistrationStub) | ||
get_gateway_device_info_handler(args, CloudRegistrationStub) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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