예제 #1
0
def test_web_vhosts():
    web_vhosts = get_vhosts()

    # Make sure we're actually getting some vhosts back
    assert(any(web_vhosts))

    # Ensure the 'staff' and 'ggroup' users have some vhosts. These are likely
    # to always stick around as they are owned by the OCF and there are
    # multiple of them present for each user
    assert(has_vhost('staff'))
    assert(has_vhost('ggroup'))

    # Check that dev-vhost.o.b.e exists as a web vhost (this is used for
    # testing by OCF staff, so it's likely to stick around). It should also
    # have a dev-host-alias.o.b.e alias present
    assert('dev-vhost.ocf.berkeley.edu' in web_vhosts)
    aliases = web_vhosts['dev-vhost.ocf.berkeley.edu']['aliases']
    assert('dev-vhost-alias.ocf.berkeley.edu' in aliases)

    flags = [vhost['flags'] for vhost in web_vhosts.values() if vhost['flags']]
    # Ensure that there's no nossl flags present, we only support SSL vhosts
    # as of rt#5347.
    assert(not any([flag_list for flag_list in flags if 'nossl' in flag_list]))
    # hsts flags are still allowed (but may become the default sometime and be
    # removed)
    assert(any([flag_list for flag_list in flags if 'hsts' in flag_list]))
예제 #2
0
def request_vhost(request: HttpRequest) -> HttpResponse:
    user = logged_in_user(request)
    attrs = user_attrs(user)
    is_group = 'callinkOid' in attrs
    error = None

    if has_vhost(user):
        return render(
            request,
            'account/vhost/already_have_vhost.html',
            {
                'title': 'You already have virtual hosting',
                'user': user,
            },
        )
    elif not eligible_for_vhost(user):
        return render(
            request,
            'account/vhost/not_eligible.html',
            {
                'title': 'You are not eligible for virtual hosting',
                'user': user,
            },
        )

    if request.method == 'POST':
        form = VirtualHostForm(is_group, request.POST)

        if form.is_valid():
            requested_subdomain = form.cleaned_data['requested_subdomain']
            university_purpose = form.cleaned_data['university_purpose']
            university_contact = form.cleaned_data['university_contact']
            comments = form.cleaned_data['comments']
            your_name = form.cleaned_data['your_name'] if is_group else attrs[
                'cn'][0]
            your_email = form.cleaned_data['your_email']
            your_position = form.cleaned_data['your_position']

            if not error:
                # send email to hostmaster@ocf and redirect to success page
                ip_addr, _ = get_client_ip(request)

                try:
                    ip_reverse = socket.gethostbyaddr(ip_addr)[0]
                except socket.herror:
                    ip_reverse = 'unknown'

                subject = 'Virtual Hosting Request: {} ({})'.format(
                    requested_subdomain,
                    user,
                )
                message = dedent('''\
                    Virtual Hosting Request:
                      - OCF Account: {user}
                      - OCF Account Title: {title}
                      - Requested Subdomain: {requested_subdomain}
                      - Current URL: https://www.ocf.berkeley.edu/~{user}/

                    University Hostmaster Questions:
                      - Purpose: {university_purpose}
                      - Contact: {university_contact}

                    Comments/Special Requests:
                    {comments}

                    Requested by:
                      - Name: {your_name}
                      - Position: {your_position}
                      - Email: {your_email}
                      - IP Address: {ip_addr} ({ip_reverse})
                      - User Agent: {user_agent}

                    --------
                    Request submitted to ocfweb ({hostname}) on {now}.
                    {full_path}''').format(
                    user=user,
                    title=attrs['cn'][0],
                    requested_subdomain=requested_subdomain,
                    university_purpose=university_purpose,
                    university_contact=university_contact,
                    comments=comments,
                    your_name=your_name,
                    your_position=your_position,
                    your_email=your_email,
                    ip_addr=ip_addr,
                    ip_reverse=ip_reverse,
                    user_agent=request.META.get('HTTP_USER_AGENT'),
                    now=datetime.datetime.now().strftime(
                        '%A %B %e, %Y @ %I:%M:%S %p', ),
                    hostname=socket.gethostname(),
                    full_path=request.build_absolute_uri(),
                )

                try:
                    send_mail(
                        '*****@*****.**' if not settings.DEBUG
                        else current_user_formatted_email(),
                        subject,
                        message,
                        sender=your_email,
                    )
                except Exception as ex:
                    # TODO: report via ocflib
                    print(ex)
                    print('Failed to send vhost request email!')
                    error = \
                        'We were unable to submit your virtual hosting ' + \
                        'request. Please try again or email us at ' + \
                        '*****@*****.**'
                else:
                    return redirect(reverse('request_vhost_success'))
    else:
        # Unsupported left operand type for + ("None") because form might not have been instantiated at this point...
        # but this doesn't matter because of if-else clause
        form = VirtualHostForm(
            is_group, initial={'requested_subdomain':
                               user + '.berkeley.edu'})  # type: ignore

    group_url = f'https://www.ocf.berkeley.edu/~{user}/'

    return render(
        request,
        'account/vhost/index.html',
        {
            'attrs': attrs,
            'error': error,
            'form': form,
            'group_url': group_url,
            'is_group': is_group,
            'title': 'Request virtual hosting',
            'user': user,
        },
    )
예제 #3
0
파일: web_test.py 프로젝트: jvperrin/ocflib
 def test_has_vhost(self, user, should_have_vhost, mock_get_vhosts_db):
     assert has_vhost(user) == should_have_vhost
예제 #4
0
파일: web_test.py 프로젝트: jvperrin/ocflib
 def test_has_vhost(self, user, should_have_vhost, mock_get_vhosts_db):
     assert has_vhost(user) == should_have_vhost
예제 #5
0
def request_vhost(request):
    user = logged_in_user(request)
    attrs = user_attrs(user)
    error = None

    if has_vhost(user):
        return render(
            request,
            'account/vhost/already_have_vhost.html',
            {
                'title': 'You already have virtual hosting',
                'user': user,
            },
        )

    if request.method == 'POST':
        form = VirtualHostForm(request.POST)

        if form.is_valid():
            requested_subdomain = form.cleaned_data['requested_subdomain']
            requested_why = form.cleaned_data['requested_why']
            comments = form.cleaned_data['comments']
            your_name = form.cleaned_data['your_name']
            your_email = form.cleaned_data['your_email']
            your_position = form.cleaned_data['your_position']

            if not error:
                # send email to hostmaster@ocf and redirect to success page
                ip_addr = get_real_ip(request)

                try:
                    ip_reverse = socket.gethostbyaddr(ip_addr)[0]
                except:
                    ip_reverse = 'unknown'

                subject = 'Virtual Hosting Request: {} ({})'.format(
                    requested_subdomain,
                    user,
                )
                message = dedent('''\
                    Virtual Hosting Request:
                      - OCF Account: {user}
                      - OCF Account Title: {title}
                      - Requested Subdomain: {requested_subdomain}
                      - Current URL: https://www.ocf.berkeley.edu/~{user}/

                    Request Reason:
                    {requested_why}

                    Comments/Special Requests:
                    {comments}

                    Requested by:
                      - Name: {your_name}
                      - Position: {your_position}
                      - Email: {your_email}
                      - IP Address: {ip_addr} ({ip_reverse})
                      - User Agent: {user_agent}

                    --------
                    Request submitted to ocfweb ({hostname}) on {now}.
                    {full_path}''').format(
                    user=user,
                    title=attrs['cn'][0],
                    requested_subdomain=requested_subdomain,
                    requested_why=requested_why,
                    comments=comments,
                    your_name=your_name,
                    your_position=your_position,
                    your_email=your_email,
                    ip_addr=ip_addr,
                    ip_reverse=ip_reverse,
                    user_agent=request.META.get('HTTP_USER_AGENT'),
                    now=datetime.datetime.now().strftime(
                        '%A %B %e, %Y @ %I:%M:%S %p',
                    ),
                    hostname=socket.gethostname(),
                    full_path=request.build_absolute_uri(),
                )

                try:
                    send_mail(
                        '*****@*****.**' if not settings.DEBUG else current_user_formatted_email(),
                        subject,
                        message,
                        sender=your_email,
                    )
                except Exception as ex:
                    # TODO: report via ocflib
                    print(ex)
                    print('Failed to send vhost request email!')
                    error = \
                        'We were unable to submit your virtual hosting ' + \
                        'request. Please try again or email us at ' + \
                        '*****@*****.**'
                else:
                    return redirect(reverse('request_vhost_success'))
    else:
        form = VirtualHostForm(initial={'requested_subdomain': user + '.berkeley.edu'})

    group_url = 'https://www.ocf.berkeley.edu/~{0}/'.format(user)

    return render(
        request,
        'account/vhost/index.html',
        {
            'attrs': attrs,
            'error': error,
            'form': form,
            'group_url': group_url,
            'title': 'Request berkeley.edu virtual hosting',
            'user': user,
        },
    )