~fo0bar/turku/turku-agent-encoding

« back to all changes in this revision

Viewing changes to turku_agent/utils.py

  • Committer: Ryan Finnie
  • Date: 2019-04-22 01:19:36 UTC
  • Revision ID: ryan.finnie@canonical.com-20190422011936-04wxzzmi9da361x1
Remove early legacy config migrations/mitigations

Show diffs side-by-side

added added

removed removed

Lines of Context:
174
174
            config['ssh_public_key'] = f.read().rstrip()
175
175
        config['ssh_public_key_file'] = os.path.join(config['var_dir'], 'ssh_key.pub')
176
176
        config['ssh_private_key_file'] = os.path.join(config['var_dir'], 'ssh_key')
177
 
    elif os.path.isfile(os.path.join(config['config_dir'], 'id_rsa.pub')):
178
 
        # XXX Legacy
179
 
        with open(os.path.join(config['config_dir'], 'id_rsa.pub')) as f:
180
 
            config['ssh_public_key'] = f.read().rstrip()
181
 
        config['ssh_public_key_file'] = os.path.join(config['config_dir'], 'id_rsa.pub')
182
 
        config['ssh_private_key_file'] = os.path.join(config['config_dir'], 'id_rsa')
183
177
 
184
178
    sources_config = {}
185
179
    # Merge in sources.d/*.json to the sources dict
293
287
        # Check for missing usernames/passwords
294
288
        if not ('username' in config['sources'][s] or 'password' in config['sources'][s]):
295
289
            sources_secrets_d = os.path.join(config['config_dir'], 'sources_secrets.d')
296
 
            if os.path.isfile(os.path.join(sources_secrets_d, s + '.json')):
297
 
                # XXX Legacy
298
 
                j = json_load_file(os.path.join(sources_secrets_d, s + '.json'))
299
 
                config['sources'][s]['username'] = j['username']
300
 
                config['sources'][s]['password'] = j['password']
301
 
            else:
302
 
                if 'username' not in config['sources'][s]:
303
 
                    config['sources'][s]['username'] = str(uuid.uuid4())
304
 
                if 'password' not in config['sources'][s]:
305
 
                    config['sources'][s]['password'] = ''.join(
306
 
                        random.choice(string.ascii_letters + string.digits)
307
 
                        for i in range(30)
308
 
                    )
 
290
            if 'username' not in config['sources'][s]:
 
291
                config['sources'][s]['username'] = str(uuid.uuid4())
 
292
            if 'password' not in config['sources'][s]:
 
293
                config['sources'][s]['password'] = ''.join(
 
294
                    random.choice(string.ascii_letters + string.digits)
 
295
                    for i in range(30)
 
296
                )
309
297
            with open(os.path.join(var_sources_d, '10-' + s + '.json'), 'w') as f:
310
298
                os.fchmod(f.fileno(), 0o600)
311
299
                json_dump_p({
316
304
                }, f)
317
305
 
318
306
 
319
 
def migrate_configs(config):
320
 
    import shutil
321
 
 
322
 
    config_d = os.path.join(config['config_dir'], 'config.d')
323
 
    sources_secrets_d = os.path.join(config['config_dir'], 'sources_secrets.d')
324
 
    var_config_d = os.path.join(config['var_dir'], 'config.d')
325
 
 
326
 
    # Avoid ETCBZR in the deployed Puppet environment
327
 
    cleanup_migration = True
328
 
    if 'cleanup_migration' in config:
329
 
        cleanup_migration = config['cleanup_migration']
330
 
 
331
 
    file_migrations = (
332
 
        (os.path.join(config['config_dir'], 'id_rsa'), os.path.join(config['var_dir'], 'ssh_key')),
333
 
        (os.path.join(config['config_dir'], 'id_rsa.pub'), os.path.join(config['var_dir'], 'ssh_key.pub')),
334
 
        (os.path.join(config_d, '10-machine_uuid.json'), os.path.join(var_config_d, '10-machine_uuid.json')),
335
 
        (os.path.join(config_d, '10-restore.json'), os.path.join(var_config_d, '10-restore.json')),
336
 
    )
337
 
 
338
 
    file_deletions = (
339
 
        os.path.join(config['config_dir'], 'rsyncd.conf'),
340
 
        os.path.join(config['config_dir'], 'rsyncd.secrets'),
341
 
        os.path.join(config['var_dir'], 'server_config.json'),
342
 
    )
343
 
 
344
 
    for src, dst in file_migrations:
345
 
        if os.path.isfile(src) and (not os.path.isfile(dst)):
346
 
            if cleanup_migration:
347
 
                shutil.move(src, dst)
348
 
            else:
349
 
                shutil.copy2(src, dst)
350
 
 
351
 
    if cleanup_migration:
352
 
        for file in file_deletions:
353
 
            if os.path.isfile(file):
354
 
                os.remove(file)
355
 
 
356
 
        # Generated secrets have already been replicated in var_sources_d
357
 
        if os.path.isdir(sources_secrets_d):
358
 
            shutil.rmtree(sources_secrets_d)
359
 
 
360
 
 
361
307
def api_call(api_url, cmd, post_data, timeout=5):
362
308
    url = urllib.parse.urlparse(api_url)
363
309
    if url.scheme == 'https':