| 1 |
#!/usr/bin/python3 |
| 2 |
|
| 3 |
# --- BEGIN COPYRIGHT BLOCK --- |
| 4 |
# Copyright (C) 2020 William Brown <[email protected]> |
| 5 |
# All rights reserved. |
| 6 |
# |
| 7 |
# License: GPL (version 3 or any later version). |
| 8 |
# See LICENSE for details. |
| 9 |
# --- END COPYRIGHT BLOCK --- |
| 10 |
# |
| 11 |
# PYTHON_ARGCOMPLETE_OK |
| 12 |
|
| 13 |
import argparse |
| 14 |
import argcomplete |
| 15 |
import json |
| 16 |
import signal |
| 17 |
import sys |
| 18 |
from lib389 import DirSrv |
| 19 |
from lib389.cli_base import ( |
| 20 |
connect_instance, |
| 21 |
disconnect_instance, |
| 22 |
setup_script_logger, |
| 23 |
format_error_to_dict, |
| 24 |
format_pretty_error) |
| 25 |
from lib389.cli_base.dsrc import dsrc_to_ldap, dsrc_arg_concat |
| 26 |
from lib389._constants import DSRC_HOME |
| 27 |
|
| 28 |
from lib389.migrate.openldap.config import olConfig, olOverlayType_from_str |
| 29 |
from lib389.migrate.ldif import LdifMetadata |
| 30 |
from lib389.migrate.plan import Migration |
| 31 |
|
| 32 |
parser = argparse.ArgumentParser( |
| 33 |
formatter_class=argparse.RawDescriptionHelpFormatter, |
| 34 |
description="""Migrate from OpenLDAP to 389 Directory Server. |
| 35 |
|
| 36 |
This command automates the process of converting an OpenLDAP server to a 389 Directory Server |
| 37 |
instance. This is a "best effort" as OpenLDAP and 389 Directory Server are not identical, |
| 38 |
so some features still may require hand migration, or can not be migrated at all. This tool |
| 39 |
intends to migrate the majority of major content such as database data, index configuration, |
| 40 |
schema and some overlays (plugins). |
| 41 |
|
| 42 |
Content we can migrate: |
| 43 |
|
| 44 |
* Schema |
| 45 |
* Database content (from ldif backup) |
| 46 |
* Database indexes |
| 47 |
* MemberOf Overlay (memberof) |
| 48 |
* Referential Integrity Overlay (refint) |
| 49 |
* Attribute Unique Overlay (unique) |
| 50 |
|
| 51 |
Some content that can *not* be migrated include some overlays (plugins), access controls |
| 52 |
and replication configuration. Examples of plugins that can not be migrated: |
| 53 |
|
| 54 |
* Access/Audit logging (built into 389-ds by default) |
| 55 |
* Chaining (Requires manual migration, may not be equivalent) |
| 56 |
* Constraints (No equivalent plugin) |
| 57 |
* Dynamic Directory Services (Requires manual migration to Class of Service Plugin) |
| 58 |
* Dynamic Groups/Lists (Requires manual migration to Roles Plugin) |
| 59 |
* Proxy Cache (No equivalent plugin, 389-ds supports read-only replicas) |
| 60 |
* Password Policy (Built into 389-ds, requires manual migration) |
| 61 |
* Rewrite/Remap (No equivalent plugin) |
| 62 |
* Sync Provider (Requires manual migration to Replication OR Content Sync Plugin) |
| 63 |
* Value Sorting (No equivalent plugin) |
| 64 |
|
| 65 |
This must be run on the server running the 389 Directory Instance as it requires filesystem |
| 66 |
access. You must run this tool as either root or dirsrv users. |
| 67 |
|
| 68 |
The following is required from your openldap instance: |
| 69 |
|
| 70 |
* slapd.d configuration directory in ldif/dynamic format |
| 71 |
* (optional) ldif file backup of the database from slapcat |
| 72 |
|
| 73 |
These can be created on the OpenLDAP host and copied to the 389 Directory Server host. No |
| 74 |
destructive actions are applied to the OpenLDAP instance. |
| 75 |
|
| 76 |
If you are already using the ldif/dynamic format from /etc/openldap/slapd.d, you should |
| 77 |
take a copy of this to use in the migration. |
| 78 |
|
| 79 |
cp -a /etc/openldap/slapd.d /root/slapd.d |
| 80 |
|
| 81 |
If you are using the slapd.conf configuration file, you can convert this to the dynamic |
| 82 |
configuration without affecting your running instance with: |
| 83 |
|
| 84 |
slaptest -f /etc/openldap/slapd.conf -F /root/slapd.d |
| 85 |
|
| 86 |
To optionally allow your database content to be migrated you may create an ldif backup of the |
| 87 |
content that 389 Directory Server can import. You must run this for each backend (suffix) |
| 88 |
of your instance with the command: |
| 89 |
|
| 90 |
# If using slapd.conf config format |
| 91 |
slapcat -f /etc/openldap/slapd.conf -b SUFFIX -l /root/suffix.ldif |
| 92 |
# If using slapd.d config format |
| 93 |
slapcat -F /etc/openldap/slapd.d -b SUFFIX -l /root/suffix.ldif |
| 94 |
|
| 95 |
You must already have a 389 Directory Server you want to import into. You can create |
| 96 |
this with the `dscreate` tool. Data and configuration in this instance WILL be |
| 97 |
modified or removed (ie potentially destructive actions). |
| 98 |
|
| 99 |
It is strongly advised you test this tool on a non-production system first to be |
| 100 |
sure the process and changes are understood. |
| 101 |
|
| 102 |
This only needs to be run on the first-instance in a 389 Directory Server topology. All |
| 103 |
other replicas should be configured from this instance post migration. |
| 104 |
""") |
| 105 |
parser.add_argument('-v', '--verbose', |
| 106 |
help="Display verbose operation tracing during command execution", |
| 107 |
action='store_true', default=False, dest='verbose') |
| 108 |
|
| 109 |
parser.add_argument('instance', |
| 110 |
help="The name of the 389-ds instance to have openldap data migrated into", |
| 111 |
) |
| 112 |
parser.add_argument('slapd_config', |
| 113 |
help="The path to the openldap slapd.d configuration that will be migrated", |
| 114 |
) |
| 115 |
parser.add_argument('slapd_ldif', nargs='*', |
| 116 |
help="The path to exported database ldifs to be imported.", |
| 117 |
) |
| 118 |
# Migration options |
| 119 |
parser.add_argument('--confirm', |
| 120 |
default=False, action='store_true', |
| 121 |
help="Confirm that you want to apply these migration actions to the 389-ds instance. By default no actions are taken." |
| 122 |
) |
| 123 |
parser.add_argument('--ignore-overlay', nargs='*', |
| 124 |
help="Ignore the following openldap overlays from having their configuration migrated to equivalent 389-ds plugins. Valid options are memberof, refint, unique.", |
| 125 |
default=[] |
| 126 |
) |
| 127 |
parser.add_argument('--ignore-schema-oid', nargs='*', |
| 128 |
help="Ignore the following openldap schema attribute or class OIDS from being migrated to 389-ds. This *may* create inconsistent schema which could cause the migration to fail. Use with caution.", |
| 129 |
default=[] |
| 130 |
) |
| 131 |
parser.add_argument('--ignore-attribute', nargs='*', |
| 132 |
help="Ignore the following attributes from entries that are loaded from the ldif. For example, you may not want to import userPassword hashes.", |
| 133 |
default=[] |
| 134 |
) |
| 135 |
# General options |
| 136 |
parser.add_argument('-D', '--binddn', |
| 137 |
help="The 389 Directory Server account to bind as for executing migration operations", |
| 138 |
default=None |
| 139 |
) |
| 140 |
parser.add_argument('-w', '--bindpw', |
| 141 |
help="Password for binddn", |
| 142 |
default=None |
| 143 |
) |
| 144 |
parser.add_argument('-W', '--prompt', |
| 145 |
action='store_true', default=False, |
| 146 |
help="Prompt for password for the bind DN" |
| 147 |
) |
| 148 |
parser.add_argument('-y', '--pwdfile', |
| 149 |
help="Specifies a file containing the password for the binddn", |
| 150 |
default=None |
| 151 |
) |
| 152 |
parser.add_argument('-Z', '--starttls', |
| 153 |
help="Connect to 389 Directory Server with StartTLS", |
| 154 |
default=False, action='store_true' |
| 155 |
) |
| 156 |
parser.add_argument('-b', '--basedn', |
| 157 |
help=argparse.SUPPRESS, |
| 158 |
default=None |
| 159 |
) |
| 160 |
parser.add_argument('-j', '--json', |
| 161 |
help=argparse.SUPPRESS, |
| 162 |
default=False, action='store_true' |
| 163 |
) |
| 164 |
|
| 165 |
# handle a control-c gracefully |
| 166 |
def signal_handler(signal, frame): |
| 167 |
print('\n\nExiting...') |
| 168 |
sys.exit(0) |
| 169 |
|
| 170 |
def do_migration(inst, log, args, skip_overlays): |
| 171 |
log.debug("do_migration -- begin preparation --") |
| 172 |
log.debug("Instance isLocal: %s" % inst.isLocal) |
| 173 |
# Do the thing |
| 174 |
|
| 175 |
# Map some args out. |
| 176 |
skip_schema_oids = args.ignore_schema_oid |
| 177 |
skip_entry_attributes = args.ignore_attribute |
| 178 |
|
| 179 |
# Parse the openldap config |
| 180 |
config = olConfig(args.slapd_config, log) |
| 181 |
|
| 182 |
# Do we have any ldifs to import? |
| 183 |
ldifmeta = LdifMetadata(args.slapd_ldif, log) |
| 184 |
|
| 185 |
# Create the migration plan. |
| 186 |
migration = Migration( |
| 187 |
inst, |
| 188 |
config.schema, |
| 189 |
config.databases, |
| 190 |
ldifmeta.get_suffixes(), |
| 191 |
skip_schema_oids, |
| 192 |
skip_overlays, |
| 193 |
skip_entry_attributes, |
| 194 |
) |
| 195 |
|
| 196 |
# Present it for review. |
| 197 |
log.debug("--> raw migration plan") |
| 198 |
log.debug(migration.__unicode__()) |
| 199 |
log.debug("<-- end raw migration plan") |
| 200 |
|
| 201 |
log.info("The following migration steps will be performed:") |
| 202 |
migration.display_plan_review(log) |
| 203 |
|
| 204 |
# Go ahead? |
| 205 |
if not args.confirm: |
| 206 |
log.info("No actions taken. To apply migration plan, use '--confirm'") |
| 207 |
return False |
| 208 |
|
| 209 |
# Do the thing. |
| 210 |
log.info("Starting Migration ... This may take some time ...") |
| 211 |
migration.execute_plan(log) |
| 212 |
log.info("🎉 Migration complete!") |
| 213 |
log.info("----------------------") |
| 214 |
log.info("You should now review your instance configuration and data:") |
| 215 |
|
| 216 |
# Display the Post Migration Checklist items. |
| 217 |
migration.display_plan_post_review(log) |
| 218 |
|
| 219 |
# Celebrate. |
| 220 |
return True |
| 221 |
|
| 222 |
if __name__ == '__main__': |
| 223 |
args = parser.parse_args() |
| 224 |
log = setup_script_logger('openldap_to_ds', args.verbose) |
| 225 |
|
| 226 |
log.debug("The Openldap to 389 Directory Server Migration Tool") |
| 227 |
# Leave this comment here: UofA let me take this code with me provided |
| 228 |
# I gave attribution. -- wibrown |
| 229 |
log.debug("Inspired by works of: ITS, The University of Adelaide") |
| 230 |
log.debug("Called with: %s", args) |
| 231 |
|
| 232 |
# Do some initial parsing. Matters for overlays. |
| 233 |
skip_overlays = [ |
| 234 |
olOverlayType_from_str(x) |
| 235 |
for x in args.ignore_overlay |
| 236 |
if olOverlayType_from_str(x) |
| 237 |
] |
| 238 |
|
| 239 |
# Now that we have our args, see how they relate with our instance. |
| 240 |
dsrc_inst = dsrc_to_ldap(DSRC_HOME, args.instance, log.getChild('dsrc')) |
| 241 |
|
| 242 |
# Now combine this with our arguments |
| 243 |
dsrc_inst = dsrc_arg_concat(args, dsrc_inst) |
| 244 |
|
| 245 |
log.debug("Instance details: %s" % dsrc_inst) |
| 246 |
|
| 247 |
inst = None |
| 248 |
result = False |
| 249 |
|
| 250 |
try: |
| 251 |
inst = connect_instance(dsrc_inst=dsrc_inst, verbose=args.verbose, args=args) |
| 252 |
result = do_migration(inst, log, args, skip_overlays) |
| 253 |
except Exception as e: |
| 254 |
log.debug(e, exc_info=True) |
| 255 |
msg = format_error_to_dict(e) |
| 256 |
if args.json: |
| 257 |
sys.stderr.write(f"{json.dumps(msg, indent=4)}\n") |
| 258 |
else: |
| 259 |
if not args.verbose: |
| 260 |
msg = format_pretty_error(msg) |
| 261 |
log.error("Error: %s" % " - ".join(str(val) for val in msg.values())) |
| 262 |
result = False |
| 263 |
disconnect_instance(inst) |
| 264 |
|
| 265 |
# Done! |
| 266 |
if result is False: |
| 267 |
sys.exit(1) |
| 268 |
|