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 Mar 3, 2022
1 parent 92f7871 commit 02a07dd
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 6 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": {},
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,7 @@ func NewRegistrationServicer() protos.RegistrationServer {
}

func (r *RegistrationService) Register(c context.Context, request *protos.RegisterRequest) (*protos.RegisterResponse, error) {
nonce, err := NonceFromToken(request.Token)
if err != nil {
return nil, err
}

deviceInfo, err := r.GetGatewayDeviceInfo(context.Background(), nonce)
deviceInfo, err := r.GetGatewayDeviceInfo(context.Background(), request.Token)
if err != nil {
clientErr := makeErr(fmt.Sprintf("could not get device info from token %v: %v", request.Token, err))
return clientErr, nil
Expand Down
27 changes: 27 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,33 @@ def reset():
"""
ServiceRegistry.get_registry()["services"] = {}

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

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.%s' % domain_name
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
124 changes: 124 additions & 0 deletions orc8r/gateway/python/scripts/register.py
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 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:
"""
Register a device and retrieves its control proxy
Args:
client: Registration stub
args: command line arguments
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()

0 comments on commit 02a07dd

Please sign in to comment.