Skip to content

Commit

Permalink
feat: Add cli for simple gateway registration
Browse files Browse the repository at this point in the history
Signed-off-by: Christine Wang <christinewang@fb.com>
  • Loading branch information
christinewang5 committed Feb 23, 2022
1 parent caf79e7 commit 08fe066
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ package unary
var identityDecoratorBypassList = map[string]struct{}{
"/magma.orc8r.Bootstrapper/GetChallenge": {},
"/magma.orc8r.Bootstrapper/RequestSign": {},
"/magma.orc8r.Registration/Register": {},
}
28 changes: 28 additions & 0 deletions orc8r/gateway/python/magma/common/service_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,34 @@ def reset():
"""
ServiceRegistry.get_registry()["services"] = {}

@staticmethod
def get_bare_rpc_channel(rootca_path=None):
"""
Return an RPC channel to bootstrap service in CLOUD, without referencing a
control proxy config.
Returns:
grpc channel
"""
ip = 'bootstrapper-controller.magma.test'
port = '7444'

rootca_path = '/var/opt/magma/certs/rootCA.pem' if not rootca_path else rootca_path
with open(rootca_path, 'rb') as rootca_f:
rootca = rootca_f.read()
ssl_creds = grpc.ssl_channel_credentials(
root_certificates=rootca,
)

authority = 'bootstrapper-controller.magma.test'
grpc_options = [('grpc.default_authority', authority)]
channel = grpc.secure_channel(
target='%s:%s' % (ip, port),
credentials=ssl_creds,
options=grpc_options,
)
return channel

@staticmethod
def get_bootstrap_rpc_channel():
"""
Expand Down
146 changes: 146 additions & 0 deletions orc8r/gateway/python/scripts/register.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
#!/usr/bin/sudo /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 subprocess
import sys
from typing import List

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,
RegisterResponse,
GetGatewayDeviceInfoRequest,
GetGatewayDeviceInfoResponse,
)
from orc8r.protos.bootstrapper_pb2_grpc import (
RegistrationStub,
CloudRegistrationStub,
)
from magma.common.service_registry import ServiceRegistry


def bare_cloud_grpc_wrapper(func):
"""
Wraps a function with a gRPC wrapper which creates a RPC client to
the service and handles any RPC Exceptions.
Usage:
@bare_grpc_wrapper
def func(client, args):
pass
func(args, ProtoStubClass, 'service')
"""

def wrapper(*alist) -> object:
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_cloud_grpc_wrapper
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()
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


def write_control_proxy(control_proxy: str):
"""
Provisions the gateway with the control proxy
Returns:
None
"""
os.system("sudo mkdir /var/opt/magma/configs")
os.system("sudo touch /var/opt/magma/configs/control_proxy.yml")
os.system("sudo chmod 777 /var/opt/magma/configs/control_proxy.yml")

with open('/var/opt/magma/configs/control_proxy.yml', 'w+') as control_proxy_f:
control_proxy_f.write(control_proxy)
control_proxy_f.close()


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(
"--no_control_proxy",
action="store_true",
help="disables writing the control proxy file"
)
args = parser.parse_args()

res = register_handler(args, RegistrationStub)

if not args.no_control_proxy:
write_control_proxy(res.control_proxy)



if __name__ == "__main__":
main()

0 comments on commit 08fe066

Please sign in to comment.